home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / asm / ira105_src.lha / ira / ira.c < prev    next >
C/C++ Source or Header  |  1995-06-09  |  91KB  |  3,501 lines

  1. ;/*
  2.    failat 20
  3.    sc gst=include:all.gst parms=register nostackcheck IRA.c
  4.    slink lib:c.o IRA.o IRA20_1.o IRA_2.o to IRA sc sd nd lib lib:sc.lib
  5.  
  6.    QUIT
  7.    Author   : Tim Ruehsen
  8.    Project  : IRA  -  68000/10/20/30/40 Interactive ReAssembler
  9.    Part     : IRA.c
  10.    Purpose  : Contains most routines and main program
  11.    Version  : $VER: IRA.c V1.05
  12.    Date     : 16.05.1995
  13.    Copyright: (C)1993-1995 Tim Ruehsen
  14. */
  15.  
  16.  
  17.  
  18. #include "IRA.h"
  19.  
  20. #if BETA == 0
  21.   UBYTE version[]="$VER: IRA V" VERSION "." REVISION " "__AMIGADATE__" (c)1993-95 Tim Ruehsen (SiliconSurfer/PHANTASM)\n\n";
  22. #else
  23.   UBYTE version[]="$VER: IRA V" VERSION "." REVISION "beta "__AMIGADATE__" (c)1993-95 Tim Ruehsen (SiliconSurfer/PHANTASM)\n\n";
  24. #endif
  25.  
  26.  
  27.  
  28.  
  29. extern
  30. char  opcode[][8],
  31.       bitshift[][4],
  32.       condcode[][3],
  33.       extension[][3],
  34.       caches[][3],
  35.       bitop[][4],
  36.       memtypename[][7],
  37.       modname[][5],
  38.       bitfield[][5],
  39.       cregname[][6];
  40.  
  41. extern
  42. UWORD cregflag[18];
  43.  
  44. extern
  45. UWORD  result[],sourceadr[],destadr[],maske[];
  46. extern
  47. UBYTE  flags[],cputype[];
  48. extern
  49. struct x_adr x_adrs[];
  50. extern
  51. UBYTE  mnebuf[],adrbuf[],dtabuf[];
  52.  
  53. extern char cpuname[][8];
  54. UWORD CPUTYPE=M68000;
  55.  
  56. UWORD  opcstart[16];
  57. UWORD  opccount[16];
  58. UWORD  opcnumber;
  59. UWORD  SIZEOF_RESULT;
  60. ULONG  ADRCOUNT;
  61.  
  62. UWORD reg1,reg2,adrmode,adrmode2,extens;
  63. ULONG displace;
  64. UWORD sigw,*buffer;
  65. UWORD extra;
  66. WORD  PASS=-1;
  67.  
  68.  
  69. UWORD *DRelocBuffer;
  70. ULONG *RelocBuffer,RelocNumber;
  71. ULONG  LastModul,FirstModul;
  72. ULONG *LabelAdr;                  /* uncorrected addresses for labels */
  73. ULONG *LabelAdr2;                 /*   corrected addresses for labels */
  74. ULONG  LabelMax=1024;
  75. ULONG  labcount;
  76. ULONG *LabelNum;
  77. ULONG *XRefListe,XRefCount;
  78. ULONG  p2labind;
  79. ULONG  LabX_len=400;
  80. ULONG *RelocAdr,*RelocVal,*RelocMod,relocount;
  81. ULONG  relocmax=1024;
  82. LONG  *RelocOff;
  83. ULONG nextreloc;
  84. UWORD *memtype;
  85.  
  86. /* needed for the -BASEREG option */
  87. UWORD  basereg= 4;
  88. ULONG  basesec=-1;
  89. LONG   baseadr;
  90.  
  91. /* needed for symbol hunks */
  92. ULONG   SymbolCount, SymbolMax=16;
  93. ULONG  *SymbolValue;
  94. UBYTE **SymbolName;
  95.  
  96. /* needed for finding data/code in code sections */
  97. ULONG  CodeAreas, CodeAreaMax=16;
  98. ULONG *CodeArea1, *CodeArea2, CodeAreaEnd;
  99. ULONG  CNFAreas, CNFAreaMax=16;
  100. ULONG *CNFArea1, *CNFArea2;
  101. ULONG  CodeAdrs, CodeAdrMax=16;
  102. ULONG *CodeAdr;
  103.  
  104. LONG   LabAdr;
  105. UWORD  LabAdrFlag;
  106.  
  107. UWORD  NearFlag;
  108.  
  109. unsigned int
  110.        sourcetype;
  111. UWORD  textmethod;
  112. ULONG  prglen,prgcount,labc1;
  113. ULONG  prgstart,prgende;
  114. ULONG  codeentry;
  115. ULONG  pc;
  116. ULONG *labelbuf;
  117.  
  118. char   configname[32];
  119. char   sourcename[128],targetname[128],tsname[128];
  120. char   binname[32],labname[32],xrefname[128];
  121.  
  122. ULONG  modulcount,modulcnt;
  123. ULONG *modultab,**modulstrt,*modultype,*moduloffs;
  124.  
  125. ULONG  pflags;
  126. LONG   adrlen;
  127.  
  128. FILE  *sourcefile,
  129.       *binfile,
  130.       *targetfile,
  131.       *labfile,
  132.       *configfile;
  133.  
  134. struct List  list;
  135. struct Node *node;
  136.  
  137. UBYTE  StdName[STDNAMELENGTH];
  138.  
  139. struct DosBase *DosBase;
  140. struct IntuitionBase *IntuitionBase;
  141.  
  142.  
  143.  
  144. extern int
  145.     GetSymbol(ULONG),
  146.     _abort(void);
  147.  
  148. extern void
  149.       *GetPMem(ULONG),
  150.       *GetNewVarBuffer(void *,ULONG),
  151.        GetLabel(LONG,UWORD),
  152.        GetXref(ULONG),
  153.        GetExtName(ULONG),
  154.        InsertXref(ULONG),
  155.        InsertLabel(LONG),
  156.        InsertReloc(ULONG,ULONG,LONG,ULONG),
  157.        InitOpcode(void),
  158.        ExamineHunks(void),
  159.        SearchRomTag(void),
  160.        WriteTarget(void *,ULONG);
  161.  
  162.  
  163. void
  164.        main(int,char *[]),
  165.        Init(void),
  166.        ExitPrg(char *, ...),
  167.        GetOpcode(void),
  168.        CheckPhase(ULONG adr),
  169.        Ausgabe(void),
  170.        WriteLabel1(ULONG),
  171.        WriteLabel2(ULONG),
  172.        ReadExecutable(void),
  173.        ReadObject(void),
  174.        ReadBinary(void),
  175.        SectionToArea(void),
  176.        InsertCodeAdr(ULONG),
  177.        InsertCodeArea(ULONG,ULONG),
  178.        InsertSymbol(UBYTE *,ULONG),
  179.        chkabort(void),
  180.        DPass0(void),
  181.        DPass1(void),
  182.        DPass2(void);
  183.  
  184. int
  185.        DoAdress2(UWORD),
  186.        DoAdress1(UWORD),
  187.        P2WriteReloc(void),
  188.        P1WriteReloc(void),
  189.        AutoScan(void);
  190.  
  191. extern ULONG
  192.        FileLength(UBYTE *),
  193.        ReadSymbol(FILE *,ULONG *,UBYTE *);
  194.  
  195. int   ARGC;
  196. char **ARGV;
  197.  
  198.  
  199. void Freemem(void *ptr,ULONG cnt)
  200. {
  201.     /* printf("Free: %08X  %8ld\n",ptr,cnt); */
  202.     FreeMem(ptr,cnt);
  203. }
  204.  
  205. void GetOpcode()
  206. {
  207. UWORD i;
  208.  
  209.     /* set the number of the opcode to the maximum (DC.W) as default. */
  210.     opcnumber=SIZEOF_RESULT/sizeof(UWORD)-1;
  211.     for(i=opcstart[sigw>>12];i<opcstart[sigw>>12]+opccount[sigw>>12];i++) {
  212.         if ((sigw&maske[i])==result[i]) {
  213.             if (cputype[i]&CPUTYPE) {
  214.                 opcnumber=i;
  215.                 break;
  216.             }
  217.         }
  218.     }
  219.  
  220.     /* split up the opcode */
  221.     reg1=(sigw&0x0e00)>>9;
  222.     reg2=(sigw&0x0007);
  223.     adrmode=(sigw&0x003f);
  224.     if (adrmode<0x38) adrmode=(adrmode>>3);
  225.     else adrmode=7+reg2;
  226.     if (flags[opcnumber]&0x80) extens=flags[opcnumber]&0x03;
  227.     else extens=(sigw&0x00c0)>>6;
  228.  
  229. }
  230.  
  231. void SearchCode(void)
  232. {
  233. ULONG i,j,end;
  234. ULONG ptr;
  235.  
  236.     end=(prgende-prgstart)/2;
  237.  
  238.     for(i=0;i<end;i++) {
  239.         if ((buffer[i]==0x4EF9) || (buffer[i]==0x4EB9)) {
  240.             /* Find JSR/JMP Abs.L */
  241.             if ((i+2)<end) {
  242.                 ptr=(buffer[i+1]<<16)+buffer[i+2];
  243.                 if (ptr>=prgstart && ptr<prgende) {
  244.                     InsertCodeAdr(ptr);
  245.                     InsertCodeAdr(i*2+prgstart);
  246.                     i=i+2;
  247.                 }
  248.             }
  249.         } else if ((buffer[i]==0x4EFA) || (buffer[i]==0x4EBA)) {
  250.             /* Find JSR/JMP lab(PC) */
  251.             if ((i+1)<end) {
  252.                 ptr=(prgstart+(i+1)*2)+(LONG)buffer[i+1];
  253.                 if (ptr>=prgstart && ptr<prgende) {
  254.                     InsertCodeAdr(ptr);
  255.                     InsertCodeAdr(i*2+prgstart);
  256.                     i=i+1;
  257.                 }
  258.             }
  259.         }
  260.     }
  261.  
  262. /*
  263.     for(i=0,j=0;j<modulcount;j++) {
  264.         end=(moduloffs[j]+modultab[j])/2;
  265.         while(i<end) {
  266.             chkabort();
  267.             if ((buffer[i]==0x6000) || (buffer[i]==0x6100)) {
  268.                 if ((i+1)<end) {
  269.                     ptr=((i+1)*2)+(WORD)buffer[i+1];
  270.                     if (ptr>=moduloffs[j] && ptr<end*2) {
  271.                         InsertCodeAdr(ptr+prgstart);
  272.                         InsertCodeAdr(i*2+prgstart);
  273.                         i=i+1;
  274.                     }
  275.                 }
  276.             }
  277.             i++;
  278.         }
  279.     }
  280. */
  281. }
  282.  
  283. void PrintAreas(void)
  284. {
  285. ULONG i;
  286.  
  287. /*    return; */
  288.  
  289.     printf("CodeAdrs: %lu   CodeAdrMax: %lu\n",CodeAdrs,CodeAdrMax);
  290.  
  291.     for(i=0;i<CodeAreas;i++)
  292.         printf("CodeArea[%lu]: %08x - %08x\n",i,CodeArea1[i],CodeArea2[i]);
  293.  
  294.     printf("\n\n");
  295.  
  296. }
  297. void CNFAreaToCodeArea(void)
  298. {
  299. ULONG i;
  300.  
  301.     for(i=0;i<CNFAreas;i++)
  302.         InsertCodeArea(CNFArea1[i],CNFArea2[i]);
  303.  
  304. }
  305.  
  306. void InsertCNFArea(ULONG adr1, ULONG adr2)
  307. {
  308. ULONG i;
  309.  
  310.     if (CNFAreas == 0) {
  311.         CNFArea1[0] = adr1;
  312.         CNFArea2[0] = adr2;
  313.         CNFAreas++;
  314.     }
  315.     else {
  316.         i=0;
  317.         while(adr1 > CNFArea2[i] && i<CNFAreas) i++;
  318.         if (adr1 == CNFArea2[i]) {
  319.             CNFArea2[i] = adr2;
  320.             while (((i+1) < CNFAreas) && (CNFArea2[i] >= CNFArea1[i+1])) {
  321.                 CNFArea2[i] = CNFArea2[i+1];
  322.                 lmovmem(&CNFArea1[i+2],&CNFArea1[i+1],CNFAreas-i-1);
  323.                 lmovmem(&CNFArea2[i+2],&CNFArea2[i+1],CNFAreas-i-1);
  324.                 CNFAreas--;
  325.                 i++;
  326.             }
  327.         }
  328.         else if ((i != CNFAreas) && (adr2 >= CNFArea1[i]))
  329.             CNFArea1[i] = adr1;
  330.         else {
  331.             lmovmem(&CNFArea1[i],&CNFArea1[i+1],CNFAreas-i);
  332.             lmovmem(&CNFArea2[i],&CNFArea2[i+1],CNFAreas-i);
  333.             CNFArea1[i] = adr1;
  334.             CNFArea2[i] = adr2;
  335.             CNFAreas++;
  336.             if (CNFAreas == CNFAreaMax) {
  337.                 CNFArea1 = GetNewVarBuffer(CNFArea1,CNFAreaMax);
  338.                 CNFArea2 = GetNewVarBuffer(CNFArea2,CNFAreaMax);
  339.                 CNFAreaMax *= 2;
  340.             }
  341.         }
  342.     }
  343. }
  344.  
  345. void CreateConfig(void)
  346. {
  347. ULONG  i;
  348. FILE  *file;
  349. ULONG  machine;
  350.  
  351. /*
  352.     while(file=fopen(configname,"r")) {
  353.         fclose(file);
  354.         strcat(configname,"1");
  355.     }
  356. */
  357.  
  358.     if (!(configfile=fopen(configname,"w")))
  359.         ExitPrg("Can't open %s",configname);
  360.  
  361.     /* Specify processor */
  362.     if (CPUTYPE==M68000) machine=68000;
  363.     if (CPUTYPE==M68010) machine=68010;
  364.     if (CPUTYPE==M68020) machine=68020;
  365.     if (CPUTYPE==M68030) machine=68030;
  366.     if (CPUTYPE==M68040) machine=68040;
  367.     if (CPUTYPE==M68060) machine=68060;
  368.     fprintf(configfile,"MACHINE %lu\n",machine);
  369.     if (CPUTYPE==M68881) machine=68881;
  370.     fprintf(configfile,"MACHINE %lu\n",machine);
  371.     if (CPUTYPE==M68851) machine=68851;
  372.     fprintf(configfile,"MACHINE %lu\n",machine);
  373.  
  374.     fprintf(configfile,"ENTRY $%08X\n",codeentry);
  375.  
  376.     fprintf(configfile,"OFFSET $%08X\n",prgstart);
  377.  
  378.     if (pflags&BASEREG2) {
  379.         fprintf(configfile,"BASEREG %u\n",basereg);
  380.         fprintf(configfile,"BASEADR $%lX\n",baseadr);
  381.         fprintf(configfile,"BASESEC %lu\n",basesec);
  382.     }
  383.  
  384.     for(i=0;i<SymbolCount;i++)
  385.         fprintf(configfile,"SYMBOL %s $%08X\n",SymbolName[i],SymbolValue[i]);
  386.  
  387.     for(i=0;i<CodeAreas;i++)
  388.         fprintf(configfile,"CODE $%08X - $%08X\n",CodeArea1[i],CodeArea2[i]);
  389.  
  390.     fputs("END\n",configfile);
  391.  
  392.     fclose(configfile);configfile=0;
  393. }
  394.  
  395. void ReadConfig(void)
  396. {
  397. ULONG area1,area2;
  398. UBYTE buffer[256],*ptr1,*ptr2;
  399. UBYTE symbol[256];
  400. ULONG value;
  401. UWORD i,j;
  402. ULONG machine;
  403.  
  404.     if (!(configfile=fopen(configname,"r"))) {
  405.         if (pflags&PREPROC) {
  406.             printf("WARNING: Can't find %s\n",configname);
  407.             return;
  408.         }
  409.         else
  410.             ExitPrg("Can't open %s",configname);
  411.     }
  412.  
  413.     do {
  414.         if (!(fgets(buffer,255,configfile))) break;
  415.         if (!strnicmp(buffer,"CODE",4)) {
  416.             if (ptr1 = strchr(buffer,'$')) stch_l(ptr1+1,(long *)&area1);
  417.             area2 = area1;
  418.             if (ptr2 = strchr(ptr1+1,'$')) stch_l(ptr2+1,(long *)&area2);
  419.             if (ptr1) {
  420.                 if (area1 < prgstart || area1 > prgende)
  421.                     ExitPrg("ERROR: %08x out of range.\n",area1);
  422.                 if (ptr2) {
  423.                     if (area2 < prgstart || area2 > prgende)
  424.                         ExitPrg("ERROR: %08x out of range (%08x-%08x).\n",area2,prgstart,prgende);
  425.                     if (area1 > area2) {
  426.                         ExitPrg("ERROR: %08x > %08x.\n",area1,area2);
  427.                     }
  428.                     else
  429.                         InsertCNFArea(area1,area2);
  430.                     if (area1 < area2) InsertCodeAdr(area1);
  431.                 }
  432.                 else
  433.                     if (area1 < prgende) InsertCodeAdr(area1);
  434.             }
  435.         }
  436.         else if(!strnicmp(buffer,"SYMBOL",6)) {
  437.             for(i=6;isspace(buffer[i]);i++);
  438.             for(j=0;isgraph(buffer[i]);) symbol[j++]=buffer[i++];
  439.             symbol[j]=0;
  440.             while(isspace(buffer[i])) i++;
  441.             if (buffer[i]=='$') stch_l(&buffer[i+1],(long *)&value);
  442.             else value = atoi(&buffer[i+1]);
  443.             if (value < prgstart || value >= prgende)
  444.                 ExitPrg("%sERROR: %s=%lu but must be within [%lu,%lu[.\n",buffer,value,prgstart,prgende);
  445.             InsertSymbol(symbol,value);
  446.         }
  447.         else if(!strnicmp(buffer,"MACHINE",7)) {
  448.             machine=atoi(&buffer[7]);
  449.             CPUTYPE=0;
  450.             if (machine==68000) CPUTYPE|=M68000;
  451.             if (machine==68010) CPUTYPE|=M68010;
  452.             if (machine==68020) CPUTYPE|=M68020;
  453.             if (machine==68030) CPUTYPE|=M68030;
  454.             if (machine==68040) CPUTYPE|=M68040;
  455.             if (machine==68060) CPUTYPE|=M68060;
  456.             if (machine==68851) CPUTYPE|=M68851;
  457.             if (machine==68881) CPUTYPE|=M68881;
  458.             if (CPUTYPE==0)
  459.                 ExitPrg("%sERROR: unknown processor.\n",buffer);
  460.         }
  461.         else if(!strnicmp(buffer,"OFFSET",6)) {
  462.             if (ptr1 = strchr(buffer,'$')) stch_l(ptr1+1,(long *)&prgstart);
  463.             else prgstart=atoi(&buffer[6]);
  464.         }
  465.         else if(!strnicmp(buffer,"ENTRY",5)) {
  466.             if (ptr1 = strchr(buffer,'$')) stch_l(ptr1+1,(long *)&codeentry);
  467.             else prgstart=atoi(&buffer[5]);
  468.         }
  469.         else if(!strnicmp(buffer,"BASEREG",7)) {
  470.             if (ptr1 = strchr(&buffer[7],'a'))
  471.                 basereg=atoi(ptr1+1);
  472.             else if (ptr1 = strchr(&buffer[7],'A'))
  473.                 basereg=atoi(ptr1+1);
  474.             else
  475.                 basereg=atoi(&buffer[7]);
  476.             if (basereg > 7 )
  477.                 ExitPrg("%sERROR: unknown address register.\n",buffer);
  478.             if (!(pflags&BASEREG2)) pflags |= BASEREG1;
  479.         }
  480.         else if(!strnicmp(buffer,"BASEADR",7)) {
  481.             if (ptr1 = strchr(buffer,'$')) stch_l(ptr1+1,(long *)&baseadr);
  482.             else baseadr=atoi(&buffer[7]);
  483.             pflags &= (~BASEREG1);
  484.             pflags |= BASEREG2;
  485.         }
  486.         else if(!strnicmp(buffer,"BASESEC",7)) {
  487.             basesec=atoi(&buffer[7]);
  488.             if (basesec >= modulcount)
  489.                 ExitPrg("%sERROR: there aren't so many sections.\n",buffer);
  490.             pflags &= (~BASEREG1);
  491.             pflags |= BASEREG2;
  492.         }
  493.     } while (strnicmp(buffer,"END",3));
  494.  
  495.     fclose(configfile);configfile=0;
  496. }
  497.  
  498. void InsertSymbol(UBYTE *name, ULONG value)
  499. {
  500. ULONG i;
  501.  
  502.     // printf("SYMBOL %s = %08X\n",name,value);
  503.  
  504.     for(i=0;i<SymbolCount;i++)
  505.         if (SymbolValue[i] == value) return;
  506.  
  507.     SymbolValue[SymbolCount] = value;
  508.     SymbolName[SymbolCount]  = GetPMem(strlen(name)+1);
  509.     strcpy(SymbolName[SymbolCount++], name);
  510.  
  511.     if (SymbolCount == SymbolMax) {
  512.         SymbolName  = GetNewVarBuffer(SymbolName,  SymbolMax);
  513.         SymbolValue = GetNewVarBuffer(SymbolValue, SymbolMax);
  514.         SymbolMax  *= 2;
  515.     }
  516. }
  517.  
  518. ULONG GetCodeAdr(ULONG *ptr)
  519. {
  520.     if (CodeAdrs) {
  521.         *ptr = CodeAdr[0];
  522.         lmovmem(&CodeAdr[1],&CodeAdr[0],CodeAdrs-1);
  523.         CodeAdrs--;
  524.         return(1);
  525.     }
  526.     return(0);
  527. }
  528.  
  529. void InsertCodeAdr(ULONG adr)
  530. {
  531. ULONG l=0,m,r=CodeAdrs,i;
  532.  
  533.     /* printf("CODEADR %08X\n",adr); */
  534.  
  535.     if (!(pflags&PREPROC)) return;
  536.  
  537.     /* check if label points into an earlier processed code area */
  538.     for(i=0;i<CodeAreas;i++) {
  539.         if ((adr >= CodeArea1[i]) && (adr < CodeArea2[i])) {
  540.             return;
  541.         }
  542.     }
  543.  
  544.     /* this case occurs pretty often */
  545.     if ((adr > CodeAdr[CodeAdrs-1]) && CodeAdrs) {
  546.         CodeAdr[CodeAdrs++] = adr;
  547.     }
  548.     else {
  549.         /* Binaeres Suchen von adr */
  550.         while (l<r) {
  551.             m=(l+r)/2;
  552.             if (CodeAdr[m] < adr) l=m+1;
  553.             else                  r=m;
  554.         }
  555.         if ((CodeAdr[r] != adr) || (r == CodeAdrs)) {
  556.             lmovmem(&CodeAdr[r],&CodeAdr[r+1],CodeAdrs-r);
  557.             CodeAdr[r] = adr;
  558.             CodeAdrs++;
  559.         }
  560.     }
  561.     if (CodeAdrs == CodeAdrMax) {
  562.         CodeAdr     = GetNewVarBuffer(CodeAdr,CodeAdrMax);
  563.         CodeAdrMax *= 2;
  564.     }
  565. }
  566.  
  567. void InsertCodeArea(ULONG adr1, ULONG adr2)
  568. {
  569. ULONG i,j;
  570.  
  571.     /* printf("ICA: %08X - %08X\n",adr1,adr2); */
  572.  
  573.     if (CodeAreas == 0) {
  574.         CodeArea1[0] = adr1;
  575.         CodeArea2[0] = adr2;
  576.         CodeAreas++;
  577.     }
  578.     else {
  579.         i=0;
  580.         while(adr1 > CodeArea2[i] && i<CodeAreas) i++;
  581.         if (adr1 == CodeArea2[i]) {
  582.             CodeArea2[i] = adr2;
  583.             while (((i+1) < CodeAreas) && (CodeArea2[i] >= CodeArea1[i+1])) {
  584.                 CodeArea2[i] = CodeArea2[i+1];
  585.                 lmovmem(&CodeArea1[i+2],&CodeArea1[i+1],CodeAreas-i-1);
  586.                 lmovmem(&CodeArea2[i+2],&CodeArea2[i+1],CodeAreas-i-1);
  587.                 CodeAreas--;
  588.                 i++;
  589.             }
  590.         }
  591.         else if ((i != CodeAreas) && (adr2 >= CodeArea1[i]))
  592.             CodeArea1[i] = adr1;
  593.         else {
  594.             lmovmem(&CodeArea1[i],&CodeArea1[i+1],CodeAreas-i);
  595.             lmovmem(&CodeArea2[i],&CodeArea2[i+1],CodeAreas-i);
  596.             CodeArea1[i] = adr1;
  597.             CodeArea2[i] = adr2;
  598.             CodeAreas++;
  599.             if (CodeAreas == CodeAreaMax) {
  600.                 CodeArea1 = GetNewVarBuffer(CodeArea1,CodeAreaMax);
  601.                 CodeArea2 = GetNewVarBuffer(CodeArea2,CodeAreaMax);
  602.                 CodeAreaMax *= 2;
  603.             }
  604.         }
  605.     }
  606.  
  607.     fprintf(stderr,"Areas: %4lu  \r",CodeAreas);
  608.     fflush(stderr);
  609.  
  610.     /* remove all labels that point within a earlier processed code area */
  611.     for(j=0;j<CodeAreas;j++) {
  612.         for(i=0;i<CodeAdrs;) {
  613.             if ((CodeAdr[i] >= CodeArea1[j]) && (CodeAdr[i] < CodeArea2[j])) {
  614.                 lmovmem(&CodeAdr[i+1],&CodeAdr[i],CodeAdrs-i-1);
  615.                 CodeAdrs--;
  616.             }
  617.             else i++;
  618.         }
  619.     }
  620. /*
  621.     printf("adr1=%08X  adr2=%08X\n",adr1,adr2);
  622.     printf("--------\n");
  623.     PrintAreas();
  624. */
  625. }
  626.  
  627. void SectionToArea(void)
  628. {
  629. ULONG i,j,ptr1;
  630.  
  631.     if (!(pflags&PREPROC)) {
  632.         for(i=0;i<modulcount;i++) {
  633.             if (modultype[i] == 0x03E9) {
  634.                 if (i==0) {
  635.                     InsertCodeArea(codeentry,moduloffs[i]+modultab[i]);
  636.                 }
  637.                 else {
  638.                     InsertCodeArea(moduloffs[i],moduloffs[i]+modultab[i]);
  639.                 }
  640.             }
  641.         }
  642.     }
  643.  
  644.     /* need at least one code area for the following algorythm */
  645.     if (CodeAreas == 0) CodeAreas = 1;
  646.  
  647.     /* splitting code areas where sections begin or end */
  648.     for(i=0;i<modulcount;i++) {
  649.         if (modultab[i] == 0) continue;
  650.         ptr1 = moduloffs[i]+modultab[i];
  651.         if (ptr1 <= CodeArea2[CodeAreas-1]) {
  652.             for(j=0;j<CodeAreas;j++) {
  653.                 if (ptr1 < CodeArea2[j]) {
  654.                     if (ptr1 == CodeArea1[j]) break;
  655.                     lmovmem(&CodeArea1[j],&CodeArea1[j+1],CodeAreas-j);
  656.                     lmovmem(&CodeArea2[j],&CodeArea2[j+1],CodeAreas-j);
  657.                     if (ptr1 < CodeArea1[j])
  658.                         CodeArea1[j] = CodeArea2[j] = ptr1;
  659.                     else if (ptr1 > CodeArea1[j])
  660.                         CodeArea2[j] = CodeArea1[j+1] = ptr1;
  661.                     CodeAreas++;
  662.                     if (CodeAreas == CodeAreaMax) {
  663.                         CodeArea1 = GetNewVarBuffer(CodeArea1,CodeAreaMax);
  664.                         CodeArea2 = GetNewVarBuffer(CodeArea2,CodeAreaMax);
  665.                         CodeAreaMax *= 2;
  666.                     }
  667.                 break;
  668.                 }
  669.             }
  670.         }
  671.         else {
  672.             CodeArea2[CodeAreas] = CodeArea1[CodeAreas] = ptr1;
  673.             CodeAreas++;
  674.             if (CodeAreas == CodeAreaMax) {
  675.                 CodeArea1 = GetNewVarBuffer(CodeArea1,CodeAreaMax);
  676.                 CodeArea2 = GetNewVarBuffer(CodeArea2,CodeAreaMax);
  677.                 CodeAreaMax *= 2;
  678.             }
  679.         }
  680.     }
  681.  
  682.     if (CodeArea1[0] != prgstart) InsertCodeArea(prgstart,prgstart);
  683. }
  684.  
  685. void DPass0(void)
  686. {
  687. UWORD  dummy;
  688. UWORD  EndFlag=0;
  689. ULONG  ptr1,ptr2,i;
  690.  
  691.     PASS = 0;
  692.     ptr2 = (prgende-prgstart)/2;
  693.     if (!(pflags&ROMTAGatZERO) && !(pflags&CONFIG)) InsertCodeAdr(codeentry);
  694.     fprintf(stderr,"Pass 0: scanning for data in code\n");
  695.  
  696. /*
  697.     for(nextreloc=0;nextreloc<relocount;nextreloc++)
  698.         if (RelocAdr[nextreloc] >= ptr1)
  699.             break;
  700. */
  701.  
  702.     while(GetCodeAdr(&ptr1)) {
  703.  
  704.         prgcount = (ptr1 - prgstart)/2;
  705.  
  706.         /* find out in which section we are */
  707.         for(modulcnt=0;modulcnt<modulcount;modulcnt++) {
  708.             if ((ptr1 >= moduloffs[modulcnt]) &&
  709.                  (ptr1 <  (moduloffs[modulcnt]+modultab[modulcnt]))) {
  710.                 CodeAreaEnd = (moduloffs[modulcnt]+modultab[modulcnt]-prgstart)/2;
  711.                 break;
  712.             }
  713.         }
  714.  
  715.         /* find the first relocation in this code area */
  716.         for(nextreloc=0;nextreloc<relocount;nextreloc++)
  717.             if (RelocAdr[nextreloc] >= ptr1)
  718.                 break;
  719.  
  720.         EndFlag = 0;
  721.         while(EndFlag == 0) {
  722.  
  723.             if (RelocAdr[nextreloc] == (prgcount*2 + prgstart)) {
  724.                 nextreloc++;
  725.                 prgcount += 2;
  726.                 continue;
  727.             }
  728.             pc = prgcount;
  729.             sigw=(UWORD)buffer[prgcount++];
  730.  
  731.  
  732.             GetOpcode();
  733.             if (flags[opcnumber]&0x20) {
  734.                 extra=buffer[prgcount];
  735.                 if (P1WriteReloc()) continue;
  736.             }
  737.  
  738.             if (opcnumber == OPC_CMPI) {
  739.                 if (CPUTYPE&M020UP) destadr[opcnumber]=0x0bfe;
  740.                 else destadr[opcnumber]=0x0bf8;
  741.             } else if (opcnumber==OPC_TST) {
  742.                 if (CPUTYPE&M020UP) sourceadr[opcnumber]=0x0fff;
  743.                 else sourceadr[opcnumber]=0x0bf8;
  744.             } else if (opcnumber==OPC_BITFIELD) {
  745.                 dummy=(sigw&0x0700)>>8;
  746.                 if (dummy==2 || dummy==4 || dummy==6 || dummy==7) sourceadr[opcnumber]=0x0a78;
  747.                 else sourceadr[opcnumber]=0x0a7e;
  748.             } else if (opcnumber==OPC_C2) {
  749.                 if (extra&0x07ff) adrmode=NOADRMODE;
  750.                 else {
  751.                     reg1=(extra&0x7000)>>12;
  752.                     if (extra&0x8000) destadr[opcnumber]=0xa001;
  753.                     else destadr[opcnumber]=0xa000;
  754.                 }
  755.             } else if (opcnumber==OPC_MOVE162) {
  756.                 switch ((buffer[prgcount]&0x0018)>>3) {
  757.                     case 0: /* (An)+,(xxx).L */
  758.                         sourceadr[opcnumber]=0x8003;
  759.                         destadr[opcnumber]  =0x8008;
  760.                         break;
  761.                     case 1: /* (xxx).L,(An)+ */
  762.                         sourceadr[opcnumber]=0x8008;
  763.                         destadr[opcnumber]  =0x8003;
  764.                         break;
  765.                     case 2: /* (An) ,(xxx).L */
  766.                         sourceadr[opcnumber]=0x8002;
  767.                         destadr[opcnumber]  =0x8008;
  768.                         break;
  769.                     case 3: /* (xxx).L, (An) */
  770.                         sourceadr[opcnumber]=0x8008;
  771.                         destadr[opcnumber]  =0x8002;
  772.                         break;
  773.                 }
  774.             } else if (opcnumber==OPC_MOVES) {
  775.                 if (extra&0x0800) {
  776.                     sourceadr[opcnumber]=0x8022;
  777.                     destadr[opcnumber]  =0x03f8;
  778.                 }
  779.                 else {
  780.                     sourceadr[opcnumber]=0x03f8;
  781.                     destadr[opcnumber]  =0x8022;
  782.                 }
  783.             }
  784.  
  785.             if ((flags[opcnumber]&0x40) && extens==3) adrmode=NOADRMODE;
  786.  
  787.             if (sourceadr[opcnumber])
  788.                 if (DoAdress1(sourceadr[opcnumber])) continue;
  789.             if (destadr[opcnumber]) {
  790.                 if (opcnumber==OPC_MOVEB || opcnumber==OPC_MOVEW || opcnumber==OPC_MOVEL) {
  791.                     adrmode=((sigw&0x01c0)>>3)|reg1;
  792.                     if (adrmode<0x38) adrmode=(adrmode>>3);
  793.                     else adrmode=7+reg1;
  794.                     reg2=reg1;
  795.                 }
  796.                 if (DoAdress1(destadr[opcnumber])) continue;
  797.                 else {
  798.                     if (opcnumber==OPC_LEA || opcnumber==OPC_MOVEAL) {
  799.                         if (pflags&BASEREG1) {
  800.                             if (adrmode2==1 && reg1==basereg)
  801.                                 printf("BASEREG\t%08X: A%hd\n",pc*2+prgstart,basereg);
  802.                         }
  803.                     }
  804.                 }
  805.             }
  806.  
  807.             /* Check for data in code */
  808.             /**************************/
  809.  
  810. /*            printf("adr=%08x  opc=%lu  adrflag=%lu\n",LabAdr,opcnumber,LabAdrFlag); */
  811.             if (LabAdrFlag == 1) {
  812.                 if (opcnumber == OPC_BCC  ||
  813.                      opcnumber == OPC_JSR  ||
  814.                      opcnumber == OPC_DBCC ||
  815.                      opcnumber == OPC_JMP  ||
  816.                      opcnumber == OPC_CALLM)
  817.                     if ((LabAdr < ptr1) || (LabAdr > (prgcount*2+prgstart)))
  818.                         InsertCodeAdr(LabAdr);
  819.                 LabAdrFlag = 0;
  820.             }
  821.             if ((((opcnumber == OPC_BCC) && (sigw&0xFF00) == 0x6000)) ||
  822.                 opcnumber == OPC_JMP ||
  823.                 opcnumber == OPC_RTS ||
  824.                 opcnumber == OPC_RTE ||
  825.                 opcnumber == OPC_RTR ||
  826.                 opcnumber == OPC_RTD ||
  827.                 opcnumber == OPC_RTM)
  828.             {
  829.                 EndFlag = 1;
  830.                 for(i=0;i<CNFAreas;i++) {
  831.                     if ((CNFArea1[i] < (prgcount*2+prgstart)) &&
  832.                          (CNFArea2[i] > (prgcount*2+prgstart))) {
  833.                         EndFlag = 0;
  834.                         break;
  835.                     }
  836.                 }
  837.                 if (EndFlag == 1)
  838.                     InsertCodeArea(ptr1, prgcount*2+prgstart);
  839.             }
  840.             if (prgcount == ptr2) {
  841.                 InsertCodeArea(ptr1, prgcount*2+prgstart);
  842.                 EndFlag = 1;
  843.             }
  844.             if (prgcount > ptr2)
  845.                 printf("Watch out: prgcount*2(=%08x) > (prgende-prgstart)(=%08x)\n",prgcount*2,prgende-prgstart);
  846.  
  847.         }
  848.  
  849.         /* Speeding up (takes out reduncies in code checking) */
  850.         for(i=0;i<CNFAreas;i++) {
  851.             if (CNFArea2[i] == (prgcount*2+prgstart)) {
  852.                 if (CNFArea1[i] <= ptr1) {
  853.                     CNFArea2[i] = ptr1;
  854.                     break;
  855.                 }
  856.             }
  857.         }
  858.     }
  859.  
  860.     fprintf(stderr,"\n");
  861.  
  862. /*
  863.     PrintAreas();
  864. */
  865.     /* preparing sections to be area aligned */
  866.     SectionToArea();
  867.  
  868. /*    printf("CodeAdrs: %lu   CodeAdrMax: %lu\n",CodeAdrs,CodeAdrMax); */
  869. /*    PrintAreas(); */
  870. }
  871.  
  872. void main(int argc,char **argv)
  873. {
  874.     ARGC = argc;
  875.     ARGV = argv;
  876.  
  877.     Init();
  878.     InitOpcode();
  879.     SearchRomTag();
  880.     if (pflags&PREPROC) {
  881.         /* SearchCode(); */
  882.         DPass0();
  883.         CreateConfig();
  884.     } else if (pflags&CONFIG) {
  885.         CNFAreaToCodeArea();
  886.     } else {
  887.         SectionToArea();
  888.     }
  889.     PrintAreas();
  890.     DPass1();
  891.     DPass2();
  892.     ExitPrg("\n");
  893. }
  894.  
  895. void DPass2()
  896. {
  897. ULONG  modtype;
  898. UWORD  tflag,text,dummy,flag;
  899. UWORD  longs_per_line;
  900. LONG   dummy1;
  901. ULONG  dummy2;
  902. ULONG  i,j,k,l,m,r,rel,zero,alpha;
  903. UBYTE *buf,*tptr;
  904. ULONG  ptr1,ptr2,end,area;
  905.  
  906.     PASS = 2;
  907.     LabelAdr2  = GetPMem(LabelMax*4+4);
  908.  
  909.     if (labcount) { /* Wenn ueberhaupt Labels vorhanden sind */
  910.         fprintf(stderr,"Pass 2: correcting labels\n");
  911.         if (!(labfile = fopen(labname,"r")))
  912.             ExitPrg("Can't open %s\n",labname);
  913.  
  914.         labelbuf = GetPMem(labc1*sizeof(ULONG));
  915.         fread(labelbuf,4,labc1,labfile);
  916.         fclose(labfile);labfile=0; 
  917.         remove(labname);
  918.         for(i=0;i<labcount;i++) {
  919.             dummy1 = LabelAdr2[i] = LabelAdr[i];
  920.             if (dummy1 < (LONG)prgstart) LabelAdr2[i]=prgstart;
  921.             for(j=0;j<CodeAreas;j++) {
  922.                 if ((dummy1>=CodeArea1[j]) && (dummy1<CodeArea2[j])) {
  923.  
  924.                     /* Binaeres Suchen von dummy1 */
  925.                     l=0;r=labc1;
  926.                     while (l<r) {
  927.                         m=(l+r)/2;
  928.                         if ((long)labelbuf[m]<dummy1) l=m+1;
  929.                         else                          r=m;
  930.                     }
  931.                     if (labelbuf[r]!=dummy1 || r==labc1) {
  932.                         if (r>0) LabelAdr2[i] = labelbuf[r-1];
  933.                         else LabelAdr2[i] = 0;
  934.                     }
  935.                     break;
  936.                 }
  937.             }
  938.         }
  939.         /* Don't free mem, or phase checking won't work !!! */
  940.         /* Freemem(labelbuf,labc1*sizeof(ULONG));labelbuf=0; */
  941.     } /* Ende der Labelbearbeitung */
  942.  
  943.  
  944.     if (textmethod) {
  945.         fprintf(stderr,"Pass 2: searching for text\n");
  946.  
  947.         for(modulcnt=0;modulcnt<modulcount;modulcnt++) {
  948.             modtype = modultype[modulcnt];
  949.             /* BSS hunk --> there is no text */
  950.             if (modtype == 0x03EB) continue;
  951.             if (!modultab[modulcnt]) continue;
  952.             buf=(UBYTE *)buffer+moduloffs[modulcnt];
  953.  
  954.             for(rel=0,i=0;i<modultab[modulcnt]-1;i++) {
  955.                 k=i;text=1;alpha=0;
  956.                 while (isprint(buf[k]) || isspace(buf[k])) {
  957.                     if (buf[k]>127) {text=0;break;}
  958.                     if (isalpha(buf[k]) && isalpha(buf[k+1])) alpha++;
  959.                     else if (alpha < 4) alpha=0;
  960.                     k++;
  961.                 }
  962.  
  963.                 /* there must be more than 4 letters concatenated */
  964.                 if (alpha < 4) {i=k;continue;}
  965.  
  966.                 /* text should be null terminated */
  967.                 if (buf[k]!=0) {i=k;continue;}
  968.  
  969.                 /* a text must have a minimum length */
  970.                 if ((k-i)<=5) {i=k;continue;}
  971.  
  972.                 /* relocations don't have to be in a text */
  973.                 while(RelocAdr[rel]<=(i+moduloffs[modulcnt]-4) && rel<relocount) rel++;
  974.                 if (rel<relocount) {
  975.                     if (RelocAdr[rel]<=(k+moduloffs[modulcnt])) {
  976.                         i=k;continue;
  977.                     }
  978.                 }
  979.  
  980.                 if (text) {
  981.  
  982.                     /* RTS --> seems to be code */
  983.                     if (buf[k-2]!=0x4E && buf[k-1]!=0x75) {
  984.                     printf("TEXT\t%08x:\n",moduloffs[modulcnt]-prgstart+i);
  985.                     printf("\tDC.B\t");
  986.                     for(tflag=0,j=i;j<=k;j++) {
  987.                         if (isprint(buf[j]) && buf[j]!='\"') {
  988.                             if (tflag==0) printf("\"%c",buf[j]);
  989.                             if (tflag==1) printf("%c",buf[j]);
  990.                             if (tflag==2) printf(",\"%c",buf[j]);
  991.                             tflag=1;
  992.                         } else {
  993.                             if (tflag==0) printf("%d",(int)buf[j]);
  994.                             if (tflag==1) printf("\",%d",(int)buf[j]);
  995.                             if (tflag==2) printf(",%d",(int)buf[j]);
  996.                             tflag=2;
  997.                         }
  998.                     }
  999.                     if (tflag==1) printf("\"\n");
  1000.                     if (tflag==2) printf("\n");
  1001.                     }
  1002.                 }
  1003.                 i=k;
  1004.             }
  1005.         }
  1006.     }
  1007.  
  1008.     fprintf(stderr,"Pass 2: writing mnemonics\n");
  1009.  
  1010.  
  1011.     if (!(targetfile = fopen(targetname,"w")))
  1012.         ExitPrg("Can't open %s\n",targetname);
  1013.  
  1014.     fprintf(targetfile,IDSTRING2,VERSION,REVISION);
  1015.     
  1016.     /* Write EQU's */
  1017.     if (XRefCount) {
  1018.         for(i=0;i<XRefCount;i++) {
  1019.             adrbuf[0]=0;
  1020.             GetExtName(i);
  1021.             if (strlen(adrbuf)<8) adrcat("\t");
  1022.             fprintf(targetfile,"%s\tEQU\t$%X\n",adrbuf,XRefListe[i]);
  1023.         }
  1024.         adrbuf[0]=0;
  1025.         fprintf(targetfile,"\n\n");
  1026.     }
  1027.  
  1028.     /* Specify processor */
  1029.     if (CPUTYPE&M68000) dummy2=68000;
  1030.     if (CPUTYPE&M68010) dummy2=68010;
  1031.     if (CPUTYPE&M68020) dummy2=68020;
  1032.     if (CPUTYPE&M68030) dummy2=68030;
  1033.     if (CPUTYPE&M68040) dummy2=68040;
  1034.     if (CPUTYPE&M68060) dummy2=68060;
  1035.     if (dummy2 != 68000) {
  1036.         fprintf(targetfile,"\tMACHINE\t%ld\n",dummy2);
  1037.     }
  1038.     if (CPUTYPE&M68881 && !CPUTYPE&(M68040|M68060))
  1039.         fprintf(targetfile,"\tFPU\n");
  1040.     if (dummy2==68020 && CPUTYPE&M68851)
  1041.         fprintf(targetfile,"\tPMMU\n");
  1042.     fprintf(targetfile,"\n");
  1043.  
  1044.     if (pflags&BASEREG2) {
  1045.         fprintf(targetfile,"\tNEAR\tA%hu,%ld\n\n",basereg,basesec);
  1046.     }
  1047.  
  1048.  
  1049.     /* If splitted, write INCLUDE directives */
  1050.     if (pflags&SPLITFILE) {
  1051.         for(modulcnt=0;modulcnt<modulcount;modulcnt++) {
  1052.             if (!modultab[modulcnt])
  1053.                 if (!(pflags&KEEP_ZEROHUNKS)) continue;
  1054.             fprintf(targetfile,"\tINCLUDE\t\"%s.S%s\"\n",targetname,itoa(modulcnt));
  1055.         }
  1056.         fprintf(targetfile,"\tEND\n");
  1057.         fclose(targetfile);targetfile=0;
  1058.     }
  1059.  
  1060.  
  1061.     prgcount = 0;
  1062.     nextreloc= 0;
  1063.     modulcnt = 0xFFFFFFFF;
  1064.  
  1065.     for(area=0;area<CodeAreas;area++) {
  1066.  
  1067.         while ((moduloffs[modulcnt+1] == CodeArea1[area]) && ((modulcnt+1) < modulcount)) {
  1068.             modulcnt++;
  1069.             modtype = modultype[modulcnt];
  1070.             if (pflags&SPLITFILE) {
  1071.                 if (targetfile) {
  1072.                     fclose(targetfile);
  1073.                 }
  1074.                 strcpy(tsname,targetname);
  1075.                 strcat(tsname,".S");
  1076.                 strcat(tsname,itoa(modulcnt));
  1077.                 if (!(targetfile = fopen(tsname,"w")))
  1078.                     ExitPrg("Can't open %s\n",tsname);
  1079.             }
  1080.  
  1081.             if ((modultab[modulcnt] != 0) || (pflags&KEEP_ZEROHUNKS)) {
  1082.                 if (memtype[modulcnt])
  1083.                     fprintf(targetfile,"\n\n\tSECTION S_%ld,%s,%s\n\n",modulcnt,modname[modtype-0x03E9],memtypename[memtype[modulcnt]]);
  1084.                 else
  1085.                     fprintf(targetfile,"\n\n\tSECTION S_%ld,%s\n\n",modulcnt,modname[modtype-0x03E9]);
  1086.                 flag = 1;
  1087.                 while(LabelAdr2[p2labind]==moduloffs[modulcnt] && p2labind<labcount) {
  1088.                     if (GetSymbol(LabelAdr[p2labind])) {
  1089.                         fprintf(targetfile,"%s:\n",adrbuf);
  1090.                         adrbuf[0]=0;
  1091.                     }
  1092.                     else flag = 0;
  1093.                     p2labind++;
  1094.                 }
  1095.                 if (flag == 0)
  1096.                     fprintf(targetfile,"SECSTRT_%ld:\n",modulcnt);
  1097.             }
  1098.         }
  1099.  
  1100.         dtabuf[0]=0;
  1101.         adrbuf[0]=0;
  1102.         mnebuf[0]=0;
  1103.  
  1104.         /* HERE BEGINS THE CODE PART OF PASS 2 */
  1105.         /***************************************/
  1106.  
  1107.         CodeAreaEnd = (CodeArea2[area]-prgstart)/2;
  1108.  
  1109.         CheckPhase (-1); /* Phasenangleich */ 
  1110.  
  1111.         while(prgcount < CodeAreaEnd) {
  1112.  
  1113.             CheckPhase(prgcount*2+prgstart);
  1114.  
  1115.             WriteLabel2(prgstart+prgcount*2);
  1116.  
  1117.             dtacat(itohex(prgstart+prgcount*2,adrlen));
  1118.             dtacat(": ");
  1119.             if (RelocAdr[nextreloc] == (prgcount*2 + prgstart)) {
  1120.                 mnecat("DC.L");
  1121.                 dtacat(itohex(buffer[prgcount],4));
  1122.                 dtacat(itohex(buffer[prgcount+1],4));
  1123.                 GetLabel(RelocVal[nextreloc],9999);
  1124.                 nextreloc++;
  1125.                 Ausgabe();
  1126.                 prgcount += 2;
  1127.                 continue;
  1128.             }
  1129.             pc = prgcount;
  1130.             sigw=buffer[prgcount++];
  1131.             dtacat(itohex(sigw,4));
  1132.  
  1133.  
  1134.             GetOpcode();
  1135.             mnecat(&opcode[opcnumber][0]);
  1136.             if (flags[opcnumber]&0x20) {
  1137.                 extra=buffer[prgcount];
  1138.                 if (P2WriteReloc()) continue;
  1139.             }
  1140.             if (flags[opcnumber]&0x10) {
  1141.                 dummy=(sigw&0x0f00)>>8;
  1142.                 if (opcnumber==OPC_BCC && dummy<2) dummy+=16;
  1143.                 mnecat(condcode[dummy]);
  1144.             }
  1145.  
  1146.  
  1147.             if (opcnumber == OPC_CMPI) {
  1148.                 if (CPUTYPE&M020UP) destadr[opcnumber]=0x0bfe;
  1149.                 else destadr[opcnumber]=0x0bf8;
  1150.             } else if (opcnumber == OPC_BITSHIFT1) {
  1151.                 /* SHIFT & ROTATE memory */
  1152.                 mnecat(bitshift[(sigw>>9)&0x0003]);
  1153.                 if (sigw&0x0100) mnecat("L");
  1154.                 else mnecat("R");
  1155.             } else if (opcnumber == OPC_BITSHIFT2) {
  1156.                 /* SHIFT & ROTATE Data Register */
  1157.                 mnecat(bitshift[(sigw>>3)&0x0003]);
  1158.                 if (sigw&0x0100) mnecat("L");
  1159.                 else mnecat("R");
  1160.                 if (sigw&0x0020) adrcat("D");
  1161.                 else {
  1162.                     adrcat("#");
  1163.                     if (!reg1) reg1=8;
  1164.                 }
  1165.                 adrcat(itohex(reg1,1));
  1166.                 adrcat(",");
  1167.             } else if (opcnumber==OPC_TST) {
  1168.                 if (CPUTYPE&M020UP) sourceadr[opcnumber]=0x0fff;
  1169.                 else sourceadr[opcnumber]=0x0bf8;
  1170.             } else if (opcnumber==OPC_BITFIELD) {
  1171.                 dummy=(sigw&0x0700)>>8;
  1172.                 mnecat(bitfield[dummy]);
  1173.                 if (dummy==2 || dummy==4 || dummy==6 || dummy==7) sourceadr[opcnumber]=0x0a78;
  1174.                 else sourceadr[opcnumber]=0x0a7e;
  1175.             } else if (opcnumber==OPC_C2) {
  1176.                 if (extra&0x07ff) adrmode=NOADRMODE;
  1177.                 else {
  1178.                     if (extra&0x0800) mnecat("HK2");
  1179.                     else mnecat("MP2");
  1180.                     reg1=(extra&0x7000)>>12;
  1181.                     if (extra&0x8000) destadr[opcnumber]=0xa001;
  1182.                     else destadr[opcnumber]=0xa000;
  1183.                 }
  1184.             } else if (opcnumber==OPC_MOVE162) {
  1185.                 switch ((buffer[prgcount]&0x0018)>>3) {
  1186.                     case 0: /* (An)+,(xxx).L */
  1187.                         sourceadr[opcnumber]=0x8003;
  1188.                         destadr[opcnumber]  =0x8008;
  1189.                         break;
  1190.                     case 1: /* (xxx).L,(An)+ */
  1191.                         sourceadr[opcnumber]=0x8008;
  1192.                         destadr[opcnumber]  =0x8003;
  1193.                         break;
  1194.                     case 2: /* (An) ,(xxx).L */
  1195.                         sourceadr[opcnumber]=0x8002;
  1196.                         destadr[opcnumber]  =0x8008;
  1197.                         break;
  1198.                     case 3: /* (xxx).L, (An) */
  1199.                         sourceadr[opcnumber]=0x8008;
  1200.                         destadr[opcnumber]  =0x8002;
  1201.                         break;
  1202.                 }
  1203.             } else if (opcnumber==OPC_MOVES) {
  1204.                 if (extra&0x0800) {
  1205.                     sourceadr[opcnumber]=0x8022;
  1206.                     destadr[opcnumber]  =0x03f8;
  1207.                 }
  1208.                 else {
  1209.                     sourceadr[opcnumber]=0x03f8;
  1210.                     destadr[opcnumber]  =0x8022;
  1211.                 }
  1212.             }
  1213.  
  1214.             if (flags[opcnumber]&0x40) {
  1215.                 if (extens!=3)
  1216.                     mnecat(extension[extens]);
  1217.                 else
  1218.                     adrmode=NOADRMODE;
  1219.             }
  1220.  
  1221.             if (sourceadr[opcnumber]) {
  1222.                 if (DoAdress2(sourceadr[opcnumber])) continue;
  1223.                 if (opcnumber!=OPC_BITFIELD)
  1224.                     if (destadr[opcnumber]) adrcat(",");
  1225.             }
  1226.             if (destadr[opcnumber]) {
  1227.                 if (opcnumber==OPC_MOVEB || opcnumber==OPC_MOVEW || opcnumber==OPC_MOVEL) {
  1228.                     adrmode=((sigw&0x01c0)>>3)|reg1;
  1229.                     if (adrmode<0x38) adrmode=(adrmode>>3);
  1230.                     else adrmode=7+reg1;
  1231.                     reg2=reg1;
  1232.                 }
  1233.                 if (DoAdress2(destadr[opcnumber])) continue;
  1234.                 if (opcnumber==OPC_PACK1 || opcnumber==OPC_PACK2 ||
  1235.                      opcnumber==OPC_UNPK1 || opcnumber==OPC_UNPK2) {
  1236.                     adrcat(",#$");
  1237.                     adrcat(itohex(extra,4));
  1238.                 }
  1239.             }
  1240.  
  1241.             if (NearFlag == 1) WriteTarget("\tFAR\n",5);
  1242.             Ausgabe();
  1243.             if (NearFlag == 1) {WriteTarget("\tNEAR\n",6);NearFlag=0;};
  1244.  
  1245.             if (prgcount > CodeAreaEnd)
  1246.                 printf("P2 Watch out: prgcount*2(=%08x) > (prgende-prgstart)(=%08x)\n",prgcount*2,prgende-prgstart);
  1247.  
  1248.         }
  1249.  
  1250.  
  1251.         while ((moduloffs[modulcnt+1] == CodeArea2[area]) && ((modulcnt+1) < modulcount)) {
  1252.             modulcnt++;
  1253.             modtype = modultype[modulcnt];
  1254.             if (pflags&SPLITFILE) {
  1255.                 if (targetfile) {
  1256.                     fclose(targetfile);
  1257.                 }
  1258.                 strcpy(tsname,targetname);
  1259.                 strcat(tsname,".S");
  1260.                 strcat(tsname,itoa(modulcnt));
  1261.                 if (!(targetfile = fopen(tsname,"w")))
  1262.                     ExitPrg("Can't open %s\n",tsname);
  1263.             }
  1264.             if ((modultab[modulcnt] != 0) || (pflags&KEEP_ZEROHUNKS)) {
  1265.                 if (memtype[modulcnt])
  1266.                     fprintf(targetfile,"\n\n\tSECTION S_%ld,%s,%s\n\n",modulcnt,modname[modtype-0x03E9],memtypename[memtype[modulcnt]]);
  1267.                 else
  1268.                     fprintf(targetfile,"\n\n\tSECTION S_%ld,%s\n\n",modulcnt,modname[modtype-0x03E9]);
  1269.                 flag = 1;
  1270.                 while(LabelAdr2[p2labind]==moduloffs[modulcnt] && p2labind<labcount) {
  1271.                     if (GetSymbol(LabelAdr[p2labind])) {
  1272.                         fprintf(targetfile,"%s:\n",adrbuf);
  1273.                         adrbuf[0]=0;
  1274.                     }
  1275.                     else flag=0;
  1276.                     p2labind++;
  1277.                 }
  1278.                 if (flag == 0)
  1279.                     fprintf(targetfile,"SECSTRT_%ld:\n",modulcnt);
  1280.             }
  1281.         }
  1282.  
  1283.         /* HERE BEGINS THE DATA PART OF PASS 2 */
  1284.         /***************************************/
  1285.  
  1286.  
  1287.         ptr1=CodeArea2[area];
  1288.         if ((area+1)<CodeAreas) 
  1289.             end = CodeArea1[area+1];
  1290.         else
  1291.             end = prgende;
  1292.  
  1293.         while (ptr1 < end) {
  1294.  
  1295.             text=0;
  1296.  
  1297.             /* write label and/or relocation */
  1298.             WriteLabel2(ptr1);
  1299.             if (RelocAdr[nextreloc] == ptr1) {
  1300.                 dtacat(itohex(ptr1,adrlen));
  1301.                 dtacat(": ");
  1302.                 dtacat(itohex(buffer[(ptr1-prgstart)/2],4));
  1303.                 dtacat(itohex(buffer[(ptr1-prgstart+2)/2],4));
  1304.                 ptr1 += 4;
  1305.                 ptr2  = ptr1;
  1306.                 mnecat("DC.L");
  1307.                 GetLabel(RelocVal[nextreloc],9999);
  1308.                 nextreloc++;
  1309.                 Ausgabe();
  1310.                 continue;
  1311.             }
  1312.  
  1313.             /* initialize upper textbound */
  1314.             ptr2=end;
  1315.             if (p2labind < labcount) ptr2=LabelAdr2[p2labind];
  1316.             if (nextreloc < relocount && RelocAdr[nextreloc] < ptr2)
  1317.                 ptr2=RelocAdr[nextreloc];
  1318.             if (end < ptr2) ptr2=end;
  1319.  
  1320.  
  1321.             buf=(UBYTE *)((ULONG)buffer+ptr1-prgstart);
  1322.  
  1323.             /* a text must have a minimum length */
  1324.             if ((ptr2-ptr1) > 4) {
  1325.  
  1326.                 /* I think a text shouldn't begin with a zero-byte */ 
  1327.                 if (buf[0]!=0) {
  1328.  
  1329.                 for(j=0,zero=0,text=1;j<(ptr2-ptr1);j++) {
  1330. /*                    if (buf[j]>127) {text=0;break;} */
  1331.                     if (buf[j]==0) {
  1332.                         if ((j+1)<(ptr2-ptr1)) {
  1333.                             if (buf[j+1]==0) {
  1334.                                 zero++;
  1335.                                 if (zero > 4) {text=0;break;}
  1336.                             }
  1337.                             else {
  1338.                                 if (text < 4) text=0;
  1339.                             }
  1340.                         }
  1341.                     } else {
  1342.                         if (!isprint(buf[j]) &&
  1343.                              !isspace(buf[j]) &&
  1344.                              buf[j] != 0x1b   &&
  1345.                              buf[j] != 0x9b)
  1346.                             {text=0;break;}
  1347.                         else {
  1348.                             text++;
  1349.                             zero=0;
  1350.                         }
  1351.                     }
  1352.                 }
  1353.                 if ((buf[j-1] != 0) && (text<6)) text=0;
  1354.                 if (text < 4) text=0;
  1355.                 if (zero > 4) text=0;
  1356.                 if (text) {
  1357.  
  1358.                     /* write buffer to file */
  1359.                     if (pflags&ADR_OUTPUT) {
  1360.                         mnecat(";");
  1361.                         mnecat(itohex(ptr1,adrlen));
  1362.                         Ausgabe();
  1363.                     }
  1364.  
  1365.  
  1366.                     if ((ptr2-ptr1) > 10000) {
  1367.                         printf("ptr1=%08x  ptr2=%08x  end=%08x\n",ptr1,ptr2,prgende);
  1368.                     }
  1369.  
  1370.                     /* get buffer for string */
  1371.                     tptr=GetPMem((ptr2-ptr1)*5+6);
  1372.  
  1373.                     if (pflags&ADR_OUTPUT) {
  1374.                         for(i=0;i<((ptr2-ptr1-1)/16+1);i++) {
  1375.                             strcpy(tptr,"\t;DC.B\t");k=7;
  1376.                             strcpy(&tptr[k++],"$");
  1377.                             strcpy(&tptr[k],itohex((ULONG)buf[i*16],2));k+=2;
  1378.                             for(j=i*16+1;j<(ptr2-ptr1) && j<((i+1)*16);j++) {
  1379.                                 strcpy(&tptr[k],",$");k+=2;
  1380.                                 strcpy(&tptr[k],itohex((ULONG)buf[j],2));k+=2;
  1381.                             }
  1382.                             tptr[k++]='\n';
  1383.                             WriteTarget(tptr,k);
  1384.                         }
  1385.                     }
  1386.  
  1387.                     /* create string */
  1388.                     for(tflag=0,j=0,k=0;j<(ptr2-ptr1);j++) {
  1389.                         if ((j==0) ||
  1390.                              (j>0 && buf[j-1]==0  && buf[j]!=0) ||
  1391.                              (j>0 && buf[j-1]==10 && buf[j]!=0 && buf[j]!=10)) {
  1392.                             if (tflag != 0) tptr[k++]='\n';
  1393.                             strcpy(&tptr[k],"\tDC.B\t");k+=6;
  1394.                             tflag=0;
  1395.                         }
  1396.                         if (isprint(buf[j])) {
  1397.                             if (tflag==0) tptr[k++]='\"';
  1398.                             if (tflag==2) {tptr[k++]=',';tptr[k++]='\"';}
  1399.                             if (buf[j]=='\"' || buf[j]=='\'') tptr[k++]='\\';
  1400.                             tptr[k++]=buf[j];
  1401.                             tflag=1;
  1402.                         } else {
  1403.                             if (tflag==1) {tptr[k++]='\"';tptr[k++]=',';}
  1404.                             if (tflag==2) tptr[k++]=',';
  1405.                             strcpy(&tptr[k],itoa((ULONG)buf[j]));
  1406.                             if (buf[j]>99) k+=3;
  1407.                             else if (buf[j]>9) k+=2;
  1408.                             else k++;
  1409.                             tflag=2;
  1410.                         }
  1411.                     }
  1412.                     if (tflag==1) tptr[k++]='\"';
  1413.                     tptr[k++]='\n';
  1414.  
  1415.                     /* write string */
  1416.                     WriteTarget(tptr,k);
  1417.  
  1418.                     /* free stringbuffer */
  1419.                     FreeMem(tptr,(ptr2-ptr1)*5+6);
  1420.  
  1421.                 }
  1422.                 }
  1423.             }
  1424.             if (text == 0) {
  1425.                 dtacat(itohex(ptr1,adrlen));
  1426.  
  1427.                 if (((ULONG)buf)&1) {
  1428.                     if ((*buf)==0) {
  1429.                         mnecat("DS.B");
  1430.                         adrcat("1");
  1431.                     } else {
  1432.                         mnecat("DC.B");
  1433.                         adrcat("$");
  1434.                         adrcat(itohex(*buf,2));
  1435.                     }
  1436.                     buf++;
  1437.                     ptr1++;
  1438.                     Ausgabe();
  1439.                 }
  1440.                 longs_per_line=0;
  1441.                 while((ptr2-ptr1)>=4) {
  1442.                     if ((*((ULONG *)buf))==0) {
  1443.                         if (longs_per_line) Ausgabe();
  1444.                         longs_per_line=0;
  1445.                         for(i=0;(ptr2-ptr1)>=4 && (*((ULONG *)buf))==0;ptr1+=4,buf+=4) i++;
  1446.                         mnecat("DS.L");
  1447.                         adrcat(itoa(i));
  1448.                         Ausgabe();
  1449.                     }
  1450.                     else {
  1451.                         if (longs_per_line == 0) {
  1452.                             mnecat("DC.L");
  1453.                             adrcat("$");
  1454.                         } else {
  1455.                             adrcat(",$");
  1456.                         }
  1457.                         adrcat(itohex(*((ULONG *)buf),8));
  1458.                         longs_per_line++;
  1459.                         buf+=4;
  1460.                         ptr1+=4;
  1461.                         if (longs_per_line == 4) {
  1462.                             longs_per_line=0;
  1463.                             Ausgabe();
  1464.                         }
  1465.                     }
  1466.                 }
  1467.                 if (longs_per_line) Ausgabe();
  1468.                 if ((ptr2-ptr1) > 1) {
  1469.                     if ((*((UWORD *)buf))==0) {
  1470.                         mnecat("DS.W");
  1471.                         adrcat("1");
  1472.                     } else {
  1473.                         mnecat("DC.W");
  1474.                         adrcat("$");
  1475.                         adrcat(itohex(*((UWORD *)buf),4));
  1476.                     }
  1477.                     buf+=2;
  1478.                     ptr1+=2;
  1479.                     Ausgabe();
  1480.                 }
  1481.                 if (ptr2-ptr1) {
  1482.                     if ((*buf)==0) {
  1483.                         mnecat("DS.B");
  1484.                         adrcat("1");
  1485.                     } else {
  1486.                         mnecat("DC.B");
  1487.                         adrcat("$");
  1488.                         adrcat(itohex(*buf,2));
  1489.                     }
  1490.                     buf++;
  1491.                     ptr1++;
  1492.                     Ausgabe();
  1493.                 }
  1494.             }
  1495.             ptr1 = ptr2;
  1496.         }
  1497.  
  1498.         prgcount = (end-prgstart)/2;
  1499.  
  1500.  
  1501.     }
  1502.  
  1503.     if (pflags&SPLITFILE) {
  1504.         fclose(targetfile);
  1505.         targetfile=0;
  1506.     }
  1507.  
  1508.     /* write last label */
  1509.     WriteLabel2(prgstart+prgcount*2);
  1510.  
  1511.     if (p2labind != labcount) {
  1512.         fprintf(stderr,"labcount=%ld  p2labind=%ld\n",labcount,p2labind);
  1513.     }
  1514.  
  1515.     if (!(pflags&SPLITFILE))
  1516.         WriteTarget("\tEND\n",5);
  1517.  
  1518.     fprintf(stderr,"100%%\n\n");
  1519. }
  1520. void CheckPhase(ULONG adr)
  1521. {
  1522. static ULONG lc=0;
  1523.  
  1524.     if (labcount) {
  1525.         if (adr == -1)
  1526.             while (labelbuf[lc] < prgcount*2+prgstart) lc++;
  1527.         else {
  1528.             if (adr != labelbuf[lc++]) printf("PHASE ERROR: adr=%08x  %08x %08x %08x\n",adr,labelbuf[lc-2],labelbuf[lc-1],labelbuf[lc]);
  1529.             while (lc<labc1 && labelbuf[lc]==labelbuf[lc-1]) lc++;
  1530.         }
  1531.     }
  1532.  
  1533. }
  1534. void WriteLabel2(ULONG adr)
  1535. {
  1536. ULONG index;
  1537. UWORD flag;
  1538. static ULONG oldadr=0;
  1539.  
  1540.     /* output of percent every 2 kb */
  1541.     if ((adr-oldadr) >= 2048) {
  1542.         fprintf(stderr,"%3d%%\r",((adr-prgstart)*100)/prglen);
  1543.         fflush(stderr);
  1544.         oldadr = adr;
  1545.     }
  1546.  
  1547.     /* Labels fuer aktuelle Adresse schreiben */
  1548.     if (LabelAdr2[p2labind]<adr && p2labind<labcount) printf("%x adr=%x This=%x\n",p2labind,adr,LabelAdr2[p2labind]);
  1549.     if (LabelAdr2[p2labind]==adr && p2labind<labcount) {
  1550.         flag = 1;index=p2labind;
  1551.         while(LabelAdr2[p2labind]==adr && p2labind<labcount) {
  1552.             if (GetSymbol(LabelAdr[p2labind])) {
  1553.                 fprintf(targetfile,"%s:\n",adrbuf);
  1554.                 adrbuf[0]=0;
  1555.             }
  1556.             else
  1557.                 flag=0;
  1558.             p2labind++;
  1559.         }
  1560.         if (flag == 0)
  1561.             fprintf(targetfile,"LAB_%04lX:\n",index);
  1562.     }
  1563. }
  1564. void Ausgabe(void)
  1565. {
  1566. WORD i;
  1567.     /* Hier findet die Ausgabe statt */
  1568.     if (pflags&ADR_OUTPUT) {
  1569.         if (dtabuf[0]) {
  1570.             i = 3-strlen(adrbuf)/8;
  1571.             if (i<=0) adrcat(" ");
  1572.             for(;i>0;i--) adrcat("\t");
  1573.             fprintf(targetfile,"\t%s\t%s;%s\n",mnebuf,adrbuf,dtabuf);
  1574.         }
  1575.         else if (adrbuf[0])
  1576.             fprintf(targetfile,"\t%s\t%s\n",mnebuf,adrbuf);
  1577.         else
  1578.             fprintf(targetfile,"\t%s\n",mnebuf);
  1579.     }
  1580.     else {
  1581.         if (adrbuf[0]) fprintf(targetfile,"\t%s\t%s\n",mnebuf,adrbuf);
  1582.         else fprintf(targetfile,"\t%s\n",mnebuf);
  1583.     }
  1584.     dtabuf[0]=0;
  1585.     adrbuf[0]=0;
  1586.     mnebuf[0]=0;
  1587. }
  1588. int P2WriteReloc()
  1589. {
  1590.     if (RelocAdr[nextreloc] == (prgcount*2 + prgstart)) {
  1591.         dtabuf[0]=0;
  1592.         mnebuf[0]=0;
  1593.         adrbuf[0]=0;
  1594.         mnecat("DC.W");
  1595.         adrcat("$");
  1596.         adrcat(itohex(sigw,4));
  1597.         dtacat(itohex(pc*2+prgstart,adrlen));
  1598.         prgcount=pc+1;
  1599.         Ausgabe();
  1600.         return(-1);
  1601.     }
  1602.     else {
  1603.         dtacat(itohex(buffer[prgcount++],4));
  1604.         return(0);
  1605.     }
  1606. }
  1607. UWORD NewAdrModes2(UWORD mode,UWORD reg)
  1608. /* AdrType :  6 --> Baseregister An */
  1609. /*           10 --> PC-relative     */
  1610. {
  1611. UWORD buf=buffer[prgcount];
  1612. UWORD scale;
  1613. UWORD bdsize;
  1614. UWORD odsize;
  1615. UWORD iis;
  1616. UWORD is;
  1617. UWORD operand,square1,square2;
  1618. LONG  adr;
  1619.  
  1620.  
  1621.     if (P2WriteReloc()) return((UWORD)-1);
  1622.  
  1623.     /* Achtung: Ungerade Offsets werden vom A68K nicht angenommen */
  1624.     if (CPUTYPE&(M68000|M68010)) {
  1625.         if (buf&0x0700) return(NOADRMODE);
  1626.         else {
  1627.             if (mode==10) {
  1628.                 adr = ((prgcount-1)*2+prgstart+(BYTE)buf);
  1629.                 if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  1630.                     return(NOADRMODE);
  1631.             }
  1632.             if (pflags&OLDSTYLE) {
  1633.                 if (mode==10) GetLabel(adr,mode);
  1634.                 else adrcat(itoa((char)(buf&0x00FF)));
  1635.                 adrcat("(");
  1636.             }
  1637.             else {
  1638.                 adrcat("(");
  1639.                 if (mode==10) GetLabel(adr,mode);
  1640.                 else adrcat(itoa((char)(buf&0x00FF)));
  1641.                 adrcat(",");
  1642.             }
  1643.             if (mode==6) {
  1644.                 adrcat("A");
  1645.                 adrcat(itohex(reg,1));
  1646.             }
  1647.             else adrcat("PC");
  1648.             if (buf&0x8000) adrcat(",A");
  1649.             else adrcat(",D");
  1650.             adrcat(itohex((buf>>12)&7,1));
  1651.             if (buf&0x0800) adrcat(".L");
  1652.             /* else adrcat(".W"); */
  1653.         }
  1654.     }
  1655.     else {
  1656.         scale =(buf&0x0600)>>9;
  1657.         if (buf&0x0100) { /* MC68020 (& up) FULL FORMAT */
  1658.             bdsize=(buf&0x0030)>>4;
  1659.             odsize=(buf&0x0003);
  1660.             iis   =(buf&0x0007);
  1661.             is    =(buf&0x0040)>>6;
  1662.             operand=square1=square2=0;
  1663.  
  1664.             if (mode==10) reg=0;
  1665.             if (buf&8)                 return(NOADRMODE);
  1666.             if (bdsize==0)             return(NOADRMODE);
  1667.             if (is==0 && iis==4)       return(NOADRMODE);
  1668.             if (is==1 && iis>=4)       return(NOADRMODE);
  1669. /*
  1670.             if (is==1 && (buf&0xfe00)) return(NOADRMODE);
  1671.             if (buf&0x0080 && reg!=0)  return(NOADRMODE);
  1672. */
  1673.             if (bdsize>1)               {operand|=1;square1|=1;}
  1674.             if (!(buf&0x0080))          {operand|=2;square1|=2;}
  1675.             if (buf&0x0080 && mode==10) {operand|=2;square1|=2;}
  1676.             if (is==0 || buf&0xF000) {
  1677.                 operand|=4;
  1678.                 if (iis<4) square1|=4;
  1679.             }
  1680.             if (odsize>1) operand|=8;
  1681.             if (iis!=0)   square2=square1;
  1682.             else          square1=0;
  1683.             operand&=~square1;
  1684.             if (!square1) operand|=6;
  1685.  
  1686.             adrcat("(");
  1687.             if (square1) adrcat("[");
  1688.             if ((square1|operand)&1) { /* base displacement */
  1689.                 if (bdsize==2) {
  1690.                     if (mode==10 && !(buf&0x0080)) {
  1691.                         adr = ((prgcount-1)*2+prgstart+(WORD)buffer[prgcount]);
  1692.                         fprintf(targetfile,"adr=%08x\n",adr);
  1693.                         if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  1694.                             return(NOADRMODE);
  1695.                         if (P2WriteReloc()) return((UWORD)-1);
  1696.                         GetLabel(adr,mode);
  1697.                     }
  1698.                     else {
  1699.                         if (P2WriteReloc()) return((UWORD)-1);
  1700.                         adrcat(itoa((WORD)buffer[prgcount-1]));
  1701.                     }
  1702.                 }
  1703.                 if (bdsize==3) {
  1704.                     if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  1705.                         GetLabel(RelocVal[nextreloc],9999);
  1706.                         nextreloc++;
  1707.                         dtacat(itohex(buffer[prgcount++],4));
  1708.                         dtacat(itohex(buffer[prgcount++],4));
  1709.                     }
  1710.                     else {
  1711.                         dtacat(itohex(buffer[prgcount++],4));
  1712.                         if (mode==10 && !(buf&0x0080)) {
  1713.                             adr = ((prgcount-2)*2+prgstart+(buffer[prgcount-1]<<16)+buffer[prgcount]);
  1714.                             if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  1715.                                 return(NOADRMODE);
  1716.                             if (P2WriteReloc()) return((UWORD)-1);
  1717.                             GetLabel(adr,mode);
  1718.                             adrcat(".L");
  1719.                         }
  1720.                         else {
  1721.                             if (P2WriteReloc()) return((UWORD)-1);
  1722.                             adrcat("$");
  1723.                             adrcat(itohex(buffer[prgcount-2],4));
  1724.                             adrcat(itohex(buffer[prgcount-1],4));
  1725.                         }
  1726.                     }
  1727.                 }
  1728.                 square1&=~1;
  1729.                 operand&=~1;
  1730.                 if (square2 && !square1) {adrcat("]");square2=0;}
  1731.                 if (square1 || operand)  adrcat(",");
  1732.             }
  1733.             /* base register or (Z)PC */
  1734.             if ((square1|operand)&2) {
  1735.                 if (buf&0x0080) adrcat("Z");
  1736.                 if (mode == 6) {
  1737.                     adrcat("A");
  1738.                     adrcat(itohex(reg,1));
  1739.                 }
  1740.                 else {
  1741.                     adrcat("PC");
  1742.                 }
  1743.                 square1&=~2;
  1744.                 operand&=~2;
  1745.                 if (square2 && !square1) {adrcat("]");square2=0;}
  1746.                 if (square1 || operand)  adrcat(",");
  1747.             }
  1748.             /* index register */
  1749.             if ((square1|operand)&4) {
  1750.                 if (is) adrcat("Z");
  1751.                 if (buf&0x8000) adrcat("A");
  1752.                 else adrcat("D");
  1753.                 adrcat(itohex((buf>>12)&7,1));
  1754.                 if (buf&0x0800) adrcat(".L");
  1755.                 /* else adrcat(".W"); */
  1756.                 if (scale) {
  1757.                     adrcat("*");
  1758.                     adrcat(itoa(1<<scale));
  1759.                 }
  1760.                 square1&=~4;
  1761.                 operand&=~4;
  1762.                 if (square2 && !square1) {adrcat("]");square2=0;}
  1763.                 if (square1 || operand)  adrcat(",");
  1764.             }
  1765.             /* outer displacement */
  1766.             if (operand&8) {
  1767.                 if (odsize==2) {
  1768.                     if (P2WriteReloc()) return((UWORD)-1);
  1769.                     adrcat(itoa((WORD)buffer[prgcount-1]));
  1770.                 }
  1771.                 if (odsize==3) {
  1772.                     if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  1773.                         GetLabel(RelocVal[nextreloc],9999);
  1774.                         nextreloc++;
  1775.                         dtacat(itohex(buffer[prgcount++],4));
  1776.                         dtacat(itohex(buffer[prgcount++],4));
  1777.                     }
  1778.                     else {
  1779.                         dtacat(itohex(buffer[prgcount++],4));
  1780.                         if (P2WriteReloc()) return((UWORD)-1);
  1781.                         adr=(buffer[prgcount-2]<<16)+buffer[prgcount-1];
  1782.                         adrcat(itoa(adr));
  1783.                         adrcat(".L");
  1784. /*
  1785.                         adrcat("$");
  1786.                         adrcat(itohex(buffer[prgcount-2],4));
  1787.                         adrcat(itohex(buffer[prgcount-1],4));
  1788. */
  1789.                     }
  1790.                 }
  1791.             }
  1792.         }
  1793.         else { /* MC68020 (& up) BRIEF FORMAT */
  1794.             if (mode==10) {
  1795.                 adr = ((prgcount-1)*2+prgstart+(BYTE)buf);
  1796.                 if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  1797.                     return(NOADRMODE);
  1798.             }
  1799.             if (pflags&OLDSTYLE) {
  1800.                 if (mode==10) GetLabel(adr,mode);
  1801.                 else adrcat(itoa((char)(buf&0x00FF)));
  1802.                 adrcat("(");
  1803.             }
  1804.             else {
  1805.                 adrcat("(");
  1806.                 if (mode==10) GetLabel(adr,mode);
  1807.                 else adrcat(itoa((char)(buf&0x00FF)));
  1808.                 adrcat(",");
  1809.             }
  1810.             if (mode==6) {
  1811.                 adrcat("A");
  1812.                 adrcat(itohex(reg,1));
  1813.             }
  1814.             else adrcat("PC");
  1815.             if (buf&0x8000) adrcat(",A");
  1816.             else adrcat(",D");
  1817.             adrcat(itohex((buf>>12)&7,1));
  1818.             if (buf&0x0800) adrcat(".L");
  1819.             /* else adrcat(".W"); */
  1820.             if (scale) {
  1821.                 adrcat("*");
  1822.                 adrcat(itoa(1<<scale));
  1823.             }
  1824.         }
  1825.     }
  1826.     adrcat(")");
  1827.     return(mode);
  1828. }
  1829.  
  1830. int DoAdress2(UWORD adrs)
  1831. /* This is for PASS 2 */
  1832. {
  1833. UWORD i;
  1834. UWORD mode=adrmode;
  1835. UWORD dummy1;
  1836. UWORD buf=buffer[prgcount];
  1837. UWORD reg,creg;
  1838. LONG  adr;
  1839.  
  1840.     if (mode!=NOADRMODE) {
  1841.         /* if (mode>0x30) mode=0x07+(mode&0x07); */
  1842.  
  1843.         if (adrs&0x2000) reg=reg1;
  1844.         else reg=reg2;
  1845.  
  1846.         if (adrs&0x8000) mode=adrs&0x00FF;
  1847.         else
  1848.             if ((adrs&0x0fff)==adrs)
  1849.                 if (!(adrs&(0x0800>>mode))) mode=NOADRMODE;
  1850.     }
  1851.  
  1852.     /* Adressierungsart bearbeiten */
  1853.     switch (mode) {
  1854.         case  0: /* Datenregister direkt */
  1855.                     adrcat("D");
  1856.                     adrcat(itohex(reg,1));
  1857.                     break;
  1858.         case  1: /* Adressregister direkt */
  1859.                     /* Auf Adressregister kann nicht byteweise zugegriffen werden    */
  1860.                     /* Bei LEA ist extens == 0 (weil ungerade Adressen erlaubt sind) */
  1861.                     if (extens || opcnumber==OPC_LEA) {
  1862.                         adrcat("A");
  1863.                         adrcat(itohex(reg,1));
  1864.                     }
  1865.                     else mode=NOADRMODE;
  1866.                     break;
  1867.         case  2: /* Adressregister indirekt */
  1868.                     adrcat("(A");
  1869.                     adrcat(itohex(reg,1));
  1870.                     adrcat(")");
  1871.                     break;
  1872.         case  3: /* (An)+  address register indirect with postincrement */
  1873.                     adrcat("(A");
  1874.                     adrcat(itohex(reg,1));
  1875.                     adrcat(")+");
  1876.                     break;
  1877.         case  4: /* Adressregister indirekt mit Predekrement */
  1878.                     adrcat("-(A");
  1879.                     adrcat(itohex(reg,1));
  1880.                     adrcat(")");
  1881.                     break;
  1882.         case  5: /* (d16,An) Adressregister indirekt mit 16Bit-Offset */
  1883.                     /* Achtung: Ungerade Offsets werden vom A68K nicht angenommen */
  1884. /*
  1885.                     if (extens && buf&1) mode=NOADRMODE;
  1886.                     else {
  1887. */
  1888.                         if (P2WriteReloc()) return(-1);
  1889.                         dummy1=0;
  1890.                         if (pflags&BASEREG2 && reg==basereg) {
  1891.                             adr = prgstart+baseadr+(WORD)buf;
  1892.                             if (adr>(LONG)(moduloffs[basesec]+modultab[basesec]-2) || adr<(LONG)moduloffs[basesec])
  1893.                                 dummy1=0;
  1894.                             else dummy1=1;
  1895.                         }
  1896.                         if (dummy1) GetLabel(adr,mode);
  1897.                         else {
  1898.                             if (pflags&OLDSTYLE) {
  1899.                                 adrcat(itoa((WORD)buf));
  1900.                                 adrcat("(A");
  1901.                             }
  1902.                             else {
  1903.                                 adrcat("(");
  1904.                                 adrcat(itoa((WORD)buf));
  1905.                                 adrcat(",A");
  1906.                             }
  1907.                             adrcat(itohex(reg,1));
  1908.                             adrcat(")");
  1909. /*                        } */
  1910.                     }
  1911.                     break;
  1912.         case  6: /* (bd,An,Xn.SIZE*SCALE) & ([bd,An,Xn.SIZE*SCALE],od) & ... */
  1913.         case 10: /* (bd,PC,Xn.SIZE*SCALE) & ([bd,PC,Xn.SIZE*SCALE],od) & ... */
  1914.                     if ((mode=NewAdrModes2(mode,reg))==-1) return(-1);
  1915.                     break;
  1916.         case  7: /* Absolute Adresse 16Bit */
  1917.                     adr = (WORD)buf;
  1918.                     if (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR || opcnumber==OPC_BITSHIFT1))
  1919.                         mode=NOADRMODE;
  1920.                     else {
  1921.                         if (P2WriteReloc()) return(-1);
  1922.                         /* adrcat("("); */
  1923.                         /* PEA  wegen den C-Proggies (Stackuebergabe) */
  1924.                         if (opcnumber == OPC_PEA)    adrcat(itoa(adr));
  1925.                         else    {
  1926.                             if (sourcetype == 1 && prgstart && (adr >= prgstart && adr <= prgende))
  1927.                                 GetLabel(adr,mode);
  1928.                             else
  1929.                                 GetXref(adr);
  1930.                         }
  1931.                         /* adrcat(").W"); */
  1932.                         adrcat(".W");
  1933.                     }
  1934.                     break;
  1935.         case  8: /* Absolute Adresse 32Bit */
  1936.                     adr = ((buf<<16) + buffer[prgcount+1]);
  1937.                     if (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR || opcnumber==OPC_BITSHIFT1))
  1938.                         mode=NOADRMODE;
  1939.                     else {
  1940.                         if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  1941.                             if ((pflags&BASEREG2) &&
  1942.                                 (RelocMod[nextreloc]==basesec))
  1943.                                 NearFlag = 1;
  1944.                             GetLabel(RelocVal[nextreloc],9999);
  1945.                             nextreloc++;
  1946.                         }
  1947.                         else {
  1948.                             /* PEA  wegen den C-Proggies (Stackuebergabe) */
  1949.                             if (opcnumber == OPC_PEA) {
  1950.                                 adrcat("$");
  1951.                                 adrcat(itohex(adr,8));
  1952.                             }
  1953.                             else {
  1954.                                 if (sourcetype == 1 && prgstart && (adr >= prgstart && adr <= prgende))
  1955.                                     GetLabel(adr,mode);
  1956.                                 else
  1957.                                     GetXref(adr);
  1958.                             }
  1959.                         }
  1960.                         dtacat(itohex(buffer[prgcount++],4));
  1961.                         dtacat(itohex(buffer[prgcount++],4));
  1962.                     }
  1963.                     break;
  1964.         case  9: /* PC - Relativ */
  1965.                     adr = (prgcount*2+prgstart+(WORD)buf);
  1966.                     if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR))) mode=NOADRMODE;
  1967.                     else {
  1968.                         if (P2WriteReloc()) return(-1);
  1969.                         if (pflags&OLDSTYLE) {
  1970.                             GetLabel(adr,mode);
  1971.                             adrcat("(PC)");
  1972.                         }
  1973.                         else {
  1974.                             adrcat("(");
  1975.                             GetLabel(adr,mode);
  1976.                             adrcat(",PC)");
  1977.                         }
  1978.                     }
  1979.                     break;
  1980.         case 11: /* IMMEDIATE */
  1981.                     if (adrs==sourceadr[opcnumber]) {
  1982.                         if (extens!=3) {
  1983.                             if (extens==0) {
  1984.                                 if (buf&0xFF00) mode=NOADRMODE;
  1985.                                 else {
  1986.                                     if (P2WriteReloc()) return(-1);
  1987.                                     adrcat("#$");
  1988.                                     adrcat(itohex(buf,2));
  1989.                                 }
  1990.                             }
  1991.                             if (extens==1) {
  1992.                                 if (P2WriteReloc()) return(-1);
  1993.                                 adrcat("#$");
  1994.                                 adrcat(itohex(buf,4));
  1995.                             }
  1996.                             if (extens==2) {
  1997.                                 /* adr = ((buf<<16) + buffer[prgcount+1]); */
  1998.                                 if (RelocAdr[nextreloc]==(prgcount*2+prgstart+2))
  1999.                                     mode=NOADRMODE;
  2000.                                 else {
  2001.                                     if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  2002.                                         adrcat("#");
  2003.                                         if ((pflags&BASEREG2) &&
  2004.                                             (RelocMod[nextreloc]==basesec))
  2005.                                             NearFlag = 1;
  2006.                                         GetLabel(RelocVal[nextreloc],9999);
  2007.                                         nextreloc++;
  2008.                                     }
  2009.                                     else {
  2010.                                         adrcat("#$");
  2011.                                         adrcat(itohex(buf,4));
  2012.                                         adrcat(itohex(buffer[prgcount+1],4));
  2013.                                     }
  2014.                                     dtacat(itohex(buffer[prgcount++],4));
  2015.                                     dtacat(itohex(buffer[prgcount++],4));
  2016.                                 }
  2017.                             }
  2018.                         }
  2019.                         else mode=NOADRMODE;
  2020.                     }
  2021.                     else {
  2022.                         if (extens==0) adrcat("CCR");
  2023.                         if (extens==1) adrcat("SR");
  2024.                         if (extens==2) mode=NOADRMODE; /* d=immediate long */
  2025.                     }
  2026.                     break;
  2027.         case 12: adrcat("CCR");
  2028.                     break;
  2029.         case 13: adrcat("SR");
  2030.                     break;
  2031.         case 14: adrcat("USP");
  2032.                     break;
  2033.         case 15: /* MOVEM */
  2034.                     if (dummy1=extra) {
  2035.                         i=0;
  2036.                         if ((opcnumber==OPC_MOVEM1 || opcnumber==OPC_MOVEM3) && !(sigw&0x0018)) {
  2037.                             while(dummy1) {
  2038.                                 if (dummy1&0x8000) {
  2039.                                     if (i<8) adrcat("D");
  2040.                                     else adrcat("A");
  2041.                                     adrcat(itohex(i&7,1));
  2042.                                     if ((dummy1&0x4000) && (i&7)<7) {
  2043.                                         adrcat("-");
  2044.                                         while((dummy1&0x4000) && (i&7)<7) {
  2045.                                             dummy1<<=1;
  2046.                                             i++;
  2047.                                         }
  2048.                                         if (i<8) adrcat("D");
  2049.                                         else adrcat("A");
  2050.                                         adrcat(itohex(i&7,1));
  2051.                                     }
  2052.                                     if ((UWORD)(dummy1<<1)) adrcat("/");
  2053.                                 }
  2054.                                 i++;
  2055.                                 dummy1<<=1;
  2056.                             }
  2057.                         }
  2058.                         else {
  2059.                             while(dummy1 || i<16) {
  2060.                                 if (dummy1&0x0001) {
  2061.                                     if (i<8) adrcat("D");
  2062.                                     else adrcat("A");
  2063.                                     adrcat(itohex(i&7,1));
  2064.                                     if ((dummy1&0x0002) && (i&7)<7) {
  2065.                                         adrcat("-");
  2066.                                         while((dummy1&0x0002) && (i&7)<7) {
  2067.                                             dummy1>>=1;
  2068.                                             i++;
  2069.                                         }
  2070.                                         if (i<8) adrcat("D");
  2071.                                         else adrcat("A");
  2072.                                         adrcat(itohex(i&7,1));
  2073.                                     }
  2074.                                     if (dummy1>>1) adrcat("/");
  2075.                                 }
  2076.                                 i++;
  2077.                                 dummy1>>=1;
  2078.                             }
  2079.                         }
  2080.                     }
  2081.                     else {
  2082.                         adrcat("(NOREG!)");
  2083.                     }
  2084.                     break;
  2085.         case 16: /* ADDQ,SUBQ */
  2086.                     adrcat("#");
  2087.                     if (!reg) reg=8;
  2088.                     adrcat(itohex(reg,1));
  2089.                     break;
  2090.         case 17: /* BKPT */
  2091.                     adrcat("#");
  2092.                     adrcat(itohex(reg,1));
  2093.                     break;
  2094.         case 18: /* DBcc */
  2095.                     adr = (prgcount*2+prgstart+(WORD)buf);
  2096.                     if (adr>(LONG)(moduloffs[modulcnt]+modultab[modulcnt]-2) || adr<(LONG)moduloffs[modulcnt] || adr&1 || !buf)
  2097.                         mode=NOADRMODE;
  2098.                     else {
  2099.                         if (P2WriteReloc()) return(-1);
  2100.                         GetLabel(adr,mode);
  2101.                     }
  2102.                     break;
  2103.         case 19: /* TRAP */
  2104.                     adrcat("#");
  2105.                     adrcat(itoa(sigw&0xF));
  2106.                     break;
  2107.         case 20: /* moveq */
  2108.                     adrcat("#");
  2109.                     adrcat(itoa((char)(sigw&0x00FF)));
  2110.                     break;
  2111.         case 21: /* Bcc */
  2112.                     if ((sigw&0x00ff)==0x00ff) {
  2113.                         if (CPUTYPE&M020UP) {
  2114.                             displace=(buf<<16)|buffer[prgcount+1];
  2115.                             if (displace!=0 && displace!=2) {
  2116.                                 displace+=prgcount*2;
  2117.                                 if (P2WriteReloc()) return(-1);
  2118.                                 if (P2WriteReloc()) return(-1);
  2119.                                 mnecat(".L");
  2120.                             }
  2121.                             else mode=NOADRMODE;
  2122.                         }
  2123.                         else mode=NOADRMODE;
  2124.                     } else if ((sigw&0x00ff)==0x0000) {
  2125.                         if (buf) {
  2126.                             displace=(prgcount*2+(WORD)(buf));
  2127.                             if (P2WriteReloc()) return(-1);
  2128.                         }
  2129.                         else mode=NOADRMODE;
  2130.                     } else {
  2131.                         mnecat(".S");
  2132.                         displace=(prgcount*2+(char)(sigw&0x00ff));
  2133.                     }
  2134.                     adr = prgstart+displace;
  2135.                     if (adr>(LONG)(moduloffs[modulcnt]+modultab[modulcnt]-2) || adr<(LONG)moduloffs[modulcnt] || adr&1)
  2136.                         mode=NOADRMODE;
  2137.                     else GetLabel(adr,mode);
  2138.                     break;
  2139.         case 22: /* LINK , RTD */
  2140.                     if (buf&1) mode=NOADRMODE;
  2141.                     else {
  2142.                         if (P2WriteReloc()) return(-1);
  2143.                         adrcat("#");
  2144.                         adrcat(itoa((WORD)buf));
  2145.                     }
  2146.                     break;
  2147.         case 23: /* BTST,BCLR,... IMMEDIATE®ISTER,SOURCEOP ONLY */
  2148.                     mnecat(&bitop[extens][0]);
  2149.                     if (!extens) destadr[opcnumber]=0x0bfe; /* BTST */
  2150.                     else destadr[opcnumber]=0x0bf8;        /* sonstige B... */
  2151.                     if (sigw&0x0100) {
  2152.                         adrcat("D");
  2153.                         adrcat(itohex(reg,1));
  2154.                     }
  2155.                     else {
  2156.                         if (P2WriteReloc()) return(-1);
  2157.                         adrcat("#");
  2158.                         if (sigw&0x0038) {
  2159.                             if (buf&0xFFF8) mode=NOADRMODE;
  2160.                         }
  2161.                         else {
  2162.                             if (buf&0xFFE0) mode=NOADRMODE;
  2163.                         }
  2164.                         adrcat(itoa(buf));
  2165.                     }
  2166.                     extens=0; /* Set extension to BYTE (undefined before) */
  2167.                     break;
  2168.         case 24: /* STOP */
  2169.                     if (P2WriteReloc()) return(-1);
  2170.                     adrcat("#$");
  2171.                     adrcat(itohex(buf,4));
  2172.                     break;
  2173.         case 25: /* BITFIELD */
  2174.                     adrcat("{");
  2175.                     reg=(extra&0x07c0)>>6;
  2176.                     if (extra&0x0800) {
  2177.                         if (reg>7) mode=NOADRMODE;
  2178.                         adrcat("D");
  2179.                     }
  2180.                     adrcat(itoa(reg));
  2181.                     adrcat(":");
  2182.                     reg=(extra&0x001F);
  2183.                     if (extra&0x0020) {
  2184.                         if (reg>7) mode=NOADRMODE;
  2185.                         adrcat("D");
  2186.                     }
  2187.                     adrcat(itoa(reg));
  2188.                     adrcat("}");
  2189.                     if (((sigw&0x0700)>>8)&1) {
  2190.                         /* BFEXTU, BFEXTS, BFFFO, BFINS */
  2191.                         if (extra&0x8000) mode=NOADRMODE;
  2192.                         adrcat(",D");
  2193.                         reg=(extra&0x7000)>>12;
  2194.                         adrcat(itoa(reg));
  2195.                     }
  2196.                     else {
  2197.                         if (extra&0xF000) mode=NOADRMODE;
  2198.                     }
  2199.                     break;
  2200.         case 26: /* RTM */
  2201.                     if (sigw&0x0008) adrcat("A");
  2202.                     else adrcat("D");
  2203.                     adrcat(itoa(reg2));
  2204.                     break;
  2205.         case 27: /* CAS2  SOURCE/DESTINATION */
  2206.                     buf=buffer[prgcount];
  2207.                     if (P2WriteReloc()) return(-1);
  2208.                     extens=(sigw&0x0600)>>9;
  2209.                     if (extens==0 || extens==1) mode=NOADRMODE;
  2210.                     else mnecat(extension[--extens]);
  2211.                     if (buf&0x0e38 || extra&0x0e38) mode=NOADRMODE;
  2212.                     else {
  2213.                         adrcat("D");
  2214.                         adrcat(itoa(extra&7));
  2215.                         adrcat(":");
  2216.                         adrcat("D");
  2217.                         adrcat(itoa(buf&7));
  2218.                         adrcat(",");
  2219.                         adrcat("D");
  2220.                         adrcat(itoa((extra&0x01c0)>>6));
  2221.                         adrcat(":");
  2222.                         adrcat("D");
  2223.                         adrcat(itoa((buf&0x01c0)>>6));
  2224.                         adrcat(",");
  2225.                         if (extra&0x8000) adrcat("(A");
  2226.                         else adrcat("(D");
  2227.                         adrcat(itoa((extra&0x7000)>>12));
  2228.                         adrcat("):(");
  2229.                         if (buf&0x8000) adrcat("(A");
  2230.                         else adrcat("(D");
  2231.                         adrcat(itoa((buf&0x7000)>>12));
  2232.                         adrcat(")");
  2233.                     }
  2234.                     break;
  2235.         case 28: /* CAS SOURCE */
  2236.                     extens=(sigw&0x0600)>>9;
  2237.                     if (extens==0) mode=NOADRMODE;
  2238.                     else mnecat(extension[--extens]);
  2239.                     if (extra&0xfe38) mode=NOADRMODE;
  2240.                     else {
  2241.                         adrcat("D");
  2242.                         adrcat(itoa(extra&7));
  2243.                         adrcat(",");
  2244.                         adrcat("D");
  2245.                         adrcat(itoa((extra&0x01c0)>>6));
  2246.                     }
  2247.                     break;
  2248.         case 29: /* DIVIDE/MULTIPLY LONG  SIGNED/UNSIGNED */
  2249.                     if (extra&0x83f8) mode=NOADRMODE;
  2250.                     else {
  2251.                         if (extra&0x0800) mnecat("S");
  2252.                         else mnecat("U");
  2253.                         if (!(extra&0x0400) && opcnumber==OPC_DIVL) mnecat("L");
  2254.                         reg=(extra&0x7000)>>12;
  2255.                         adrcat("D");
  2256.                         if (reg==(extra&0x0007)) {
  2257.                             if (opcnumber==OPC_MULL) mode=NOADRMODE;
  2258.                             else adrcat(itoa(reg));
  2259.                         }
  2260.                         else {                        
  2261.                             adrcat(itoa(extra&0x0007));
  2262.                             adrcat(":D");
  2263.                             adrcat(itoa(reg));
  2264.                         }
  2265.                         mnecat(".L");
  2266.                     }
  2267.                     break;
  2268.         case 30: /* LINK LONG */
  2269.                     displace=(buf<<16)|buffer[prgcount+1];
  2270.                     if (displace&1) mode=NOADRMODE;
  2271.                     else {
  2272.                         if (P2WriteReloc()) return(-1);
  2273.                         if (P2WriteReloc()) return(-1);
  2274.                         adrcat("#");
  2275.                         adrcat(itoa(displace));
  2276.                     }
  2277.                     break;
  2278.         case 31: /* MOVE16 POSTINCREMENT ONLY (DESTINATION) */
  2279.                     if ((buf&0x8fff)!=0x8000) mode=NOADRMODE;
  2280.                     else {
  2281.                         if (P2WriteReloc()) return(-1);
  2282.                         adrcat("(A");
  2283.                         adrcat(itoa((buf&0x7000)>>12));
  2284.                         adrcat(")+");
  2285.                     }
  2286.                     break;
  2287.         case 32: /* CINV & CPUSH */
  2288.                     if (sigw&0x0020) mnecat("PUSH");
  2289.                     else mnecat("INV");
  2290.                     destadr[opcnumber]=0x8002;
  2291.                     switch ((sigw&0x0018)>>3) {
  2292.                         case 0:
  2293.                             mode=NOADRMODE;
  2294.                             break;
  2295.                         case 1:
  2296.                             mnecat("L");
  2297.                             break;
  2298.                         case 2:
  2299.                             mnecat("P");
  2300.                             break;
  2301.                         case 3:
  2302.                             if (sigw&7) mode=NOADRMODE;
  2303.                             else {
  2304.                                 mnecat("A");
  2305.                                 destadr[opcnumber]=0x0000;
  2306.                             }
  2307.                             break;
  2308.                     }
  2309.                     adrcat(caches[(sigw&0x00c0)>>6]);
  2310.                     break;
  2311.         case 33: /* MOVEC */
  2312.                     if (P2WriteReloc()) return(-1);
  2313.                     reg =(buf&0x7000)>>12;
  2314.                     creg=buf&0x0fff;
  2315.                     if (creg&0x07f8) mode=NOADRMODE;
  2316.                     else {
  2317.                         if (sigw&1) {
  2318.                             if (buf&0x8000) adrcat("A");
  2319.                             else adrcat("D");
  2320.                             adrcat(itoa(reg));
  2321.                             adrcat(",");
  2322.                         }
  2323.                         if (creg&0x0800) creg=(creg%8)+9;
  2324.                         if (CPUTYPE&cregflag[creg]) adrcat(cregname[creg]);
  2325.                         else mode=NOADRMODE;
  2326.                         if (!(sigw&1)) {
  2327.                             adrcat(",");
  2328.                             if (buf&0x8000) adrcat("A");
  2329.                             else adrcat("D");
  2330.                             adrcat(itoa(reg));
  2331.                         }
  2332.                     }
  2333.                     break;
  2334.             case 34: /* MOVES */
  2335.                     if (extra&0x07ff) mode=NOADRMODE;
  2336.                     else {
  2337.                         reg=(extra&0x7000)>>12;
  2338.                         if (extra&0x8000) adrcat("A");
  2339.                         else adrcat("D");
  2340.                         adrcat(itoa(reg));
  2341.                     }
  2342.                     break;
  2343.     }
  2344.     if (prgcount > CodeAreaEnd) mode=NOADRMODE;
  2345.     if (mode==NOADRMODE) {
  2346.         NearFlag =0;
  2347.         adrbuf[0]=0;
  2348.         mnebuf[0]=0;
  2349.         dtabuf[0]=0;
  2350.         mnecat("DC.W");
  2351.         adrcat("$");
  2352.         adrcat(itohex(sigw,4));
  2353.         dtacat(itohex(pc*2+prgstart,adrlen));
  2354.         prgcount = pc+1;
  2355.         Ausgabe();
  2356.         return(-1);
  2357.     }
  2358.     return(0);
  2359. }
  2360. void FormatError(void)
  2361. {
  2362.         fprintf(stderr,"Usage  : IRA");
  2363.         fprintf(stderr," [options] <Source> [Target]\n\n");
  2364.         fprintf(stderr,"Source : ");
  2365.         fprintf(stderr,"\tSpecifies the path of the source.\n");
  2366.         fprintf(stderr,"Target : ");
  2367.         fprintf(stderr,"\tSpecifies the path of the target.\n");
  2368.         fprintf(stderr,"Options:\n");
  2369.         fprintf(stderr,"\t-M680x0\t\tx = 0,1,2,3,4: Specifies processor.\n");
  2370.         fprintf(stderr,"\t-BINARY\t\tTreat sourcefile as binary.\n");
  2371.         fprintf(stderr,"\t-a\t\tAppend address and data to every line.\n");
  2372.         fprintf(stderr,"\t-INFO\t\tPrint information about the hunkstructure.\n");
  2373.         fprintf(stderr,"\t-OFFSET=<offset>\tSpecifies offset to relocate at.\n");
  2374.         fprintf(stderr,"\t-TEXT=<x>\t\tx = 1: Method for searching text.\n");
  2375.         fprintf(stderr,"\t-KEEPZH\t\tHunks with zero length are recognised.\n");
  2376.         fprintf(stderr,"\t-KEEPBIN\tKeep the file with the binary data.\n");
  2377.         fprintf(stderr,"\t-OLDSTYLE\tAdressing modes are M68000 like.\n");
  2378.         fprintf(stderr,"\t-NEWSTYLE\tAdressing modes are M68020 like.\n");
  2379.         fprintf(stderr,"\t-SPLITFILE\tPut each section in its own file.\n");
  2380.         fprintf(stderr,"\t-CONFIG\t\tLoads configfile.\n");
  2381.         fprintf(stderr,"\t-PREPROC\tFinds data in code sections. Useful.\n");
  2382.         fprintf(stderr,"\t-ENTRY=<offset>\t\tWhere to begin scanning of code.\n");
  2383.         fprintf(stderr,"\t-BASEREG[=<x>,<adr>,<sec>]\n");
  2384.         fprintf(stderr,"\t\t\tBaserelative mode d16(Ax).\n");
  2385.         fprintf(stderr,"\t\t\tx = 0-7: Number of the address register.\n");
  2386.         fprintf(stderr,"\t\t\tadr    : Base address.\n");
  2387.         fprintf(stderr,"\t\t\tsec    : Section accessed by d16(Ax).\n\n");
  2388.         ExitPrg(0);
  2389. }
  2390. void Init(void)
  2391. {
  2392. ULONG  i;
  2393. UBYTE  zwbuf[80];
  2394. char  *odata,option,*data;
  2395. int    nextarg=1;
  2396. UWORD  argflag=0,errflag=0;
  2397.  
  2398.     NewList(&list);
  2399.  
  2400.     if (!ARGC) exit(0); /* Workbench wird noch nicht unterstuetzt */
  2401.  
  2402.     if (onbreak(_abort)) exit(0);
  2403.  
  2404.     fprintf(stderr,IDSTRING1,VERSION,REVISION);
  2405.  
  2406.     if (ARGC < 2) FormatError();
  2407.  
  2408.     while(odata=argopt(ARGC,ARGV,"",&nextarg,&option)) {
  2409.         switch (option) {
  2410.             case  'e':
  2411.             case  'E':
  2412.                 if (!strnicmp(odata,"NTRY=",5)) {
  2413.                     if (odata[5]=='$') stch_l(&odata[6],(long *)&codeentry);
  2414.                     else stcd_l(&odata[5],(long *)&codeentry);
  2415.                     if ((LONG)codeentry < 0L) {
  2416.                         printf("-ENTRY: ENTRY must not be negativ!\n");
  2417.                         errflag=1;
  2418.                     }
  2419.                     break;
  2420.                 }
  2421.                 errflag=1;
  2422.                 break;
  2423.             case  's':
  2424.             case  'S':
  2425.                 if (!(stricmp(odata,"PLITFILE"))) {pflags |= SPLITFILE;break;}
  2426.                 errflag=1;
  2427.                 break;
  2428.             case  'f':
  2429.             case  'F':
  2430.                 if (!(stricmp(odata,"ORCECODE"))) {pflags |= FORCECODE;break;}
  2431.                 errflag=1;
  2432.                 break;
  2433.             case  'p':
  2434.             case  'P':
  2435.                 if (!(stricmp(odata,"REPROC"))) {pflags |= PREPROC;break;}
  2436.                 errflag=1;
  2437.                 break;
  2438.             case  't':
  2439.             case  'T':
  2440.                 if (!(stricmp(odata,"EXT=1"))) {textmethod=1;break;}
  2441.                 errflag=1;
  2442.                 break;
  2443.             case  'm':
  2444.             case  'M':
  2445.                 if (!strcmp(odata,"68000")) {CPUTYPE|=M68000;break;}
  2446.                 if (!strcmp(odata,"68010")) {CPUTYPE|=M68010;break;}
  2447.                 if (!strcmp(odata,"68020")) {CPUTYPE|=M68020;break;}
  2448.                 if (!strcmp(odata,"68030")) {CPUTYPE|=M68030;break;}
  2449.                 if (!strcmp(odata,"68040")) {CPUTYPE|=M68040;break;}
  2450.                 if (!strcmp(odata,"68060")) {CPUTYPE|=M68060;break;}
  2451.                 if (!strcmp(odata,"68881")) {CPUTYPE|=M68881;break;}
  2452.                 if (!strcmp(odata,"68851")) {CPUTYPE|=M68851;break;}
  2453.                 errflag=1;
  2454.                 break;
  2455.             case  'a':
  2456.             case  'A':
  2457.                 pflags |= ADR_OUTPUT;
  2458.                 break;
  2459.             case  'O':
  2460.             case  'o':
  2461.                 if (!stricmp(odata,"LDSTYLE")) {argflag=1;break;}
  2462.                 if (!strnicmp(odata,"FFSET=",6)) {
  2463.                     if (odata[6]=='$') stch_l(&odata[7],(long *)&prgstart);
  2464.                     else stcd_l(&odata[6],(long *)&prgstart);
  2465.                     if ((LONG)prgstart < 0L) {
  2466.                         printf("-OFFSET: OFFSET must not be negativ!\n");
  2467.                         errflag=1;
  2468.                     }
  2469.                 }
  2470.                 break;
  2471.             case  'I':
  2472.             case  'i':
  2473.                 if (!(strnicmp(odata,"NFO",3))) pflags |= SHOW_RELOCINFO;
  2474.                 break;
  2475.             case  'C':
  2476.             case  'c':
  2477.                 if (!(strnicmp(odata,"ONFIG",5))) {
  2478.                     pflags |= CONFIG;
  2479.                     break;
  2480.                 }
  2481.                 errflag=1;
  2482.                 break;
  2483.             case  'k':
  2484.             case  'K':
  2485.                 if (!(stricmp(odata,"EEPZH")))  {pflags |= KEEP_ZEROHUNKS;break;}
  2486.                 if (!(stricmp(odata,"EEPBIN"))) {pflags |= KEEP_BINARY;break;}
  2487.                 errflag=1;
  2488.                 break;
  2489.             case  'n':
  2490.             case  'N':
  2491.                 if (!(stricmp(odata,"EWSTYLE"))) {argflag=2;break;}
  2492.                 errflag=1;
  2493.                 break;
  2494.             case  'b':
  2495.             case  'B':
  2496.                 if (!(stricmp(odata,"INARY")))  {sourcetype=1;break;}
  2497.                 if (!(stricmp(odata,"ASEREG"))) {pflags |= BASEREG1;break;}
  2498.                 if (!(strnicmp(odata,"ASEREG=",7))) {
  2499.                     basereg=odata[7]-'0';
  2500.                     if (data=strchr(odata,',')) {
  2501.                         if (data[1]=='$') stch_l(&data[2],(long *)&baseadr);
  2502.                         else stcd_l(&data[1],(long *)&baseadr);
  2503.                         pflags |= BASEREG2;
  2504.                         if (data=strchr(&data[1],',')) {
  2505.                             stcd_l(&data[1],(long *)&basesec);
  2506.                         }
  2507.                     }
  2508.                     else pflags |= BASEREG1;
  2509.                     if (basereg > 7) errflag=1;
  2510.                     break;
  2511.                 }
  2512.                 errflag=1;
  2513.                 break;
  2514.             default:
  2515.                 errflag=1;
  2516.                 break;
  2517.         }
  2518.     }
  2519.  
  2520.     if (errflag==1) FormatError();
  2521.  
  2522.     if (CPUTYPE&(M68000|M68010)) pflags|=OLDSTYLE;
  2523.     if (argflag==1) pflags|= OLDSTYLE;
  2524.     if (argflag==2) pflags&=~OLDSTYLE;
  2525.  
  2526.     if (nextarg < ARGC)
  2527.         strcpy(sourcename,ARGV[nextarg++]);
  2528.     else
  2529.         ExitPrg("No source specified!\n");
  2530.  
  2531.     if (nextarg < ARGC)
  2532.         strcpy(targetname,ARGV[nextarg]);
  2533.     else {
  2534.         strsfn(sourcename,0,0,targetname,0);
  2535.         strcat(targetname,".asm");
  2536.         while (!stricmp(sourcename,targetname))
  2537.             strcat(targetname,"1");
  2538.     }
  2539.  
  2540.     strsfn(sourcename,0,0,configname,0);
  2541.     strcat(configname,".cnf");
  2542.  
  2543.     strsfn(sourcename,0,0,binname,0);
  2544.     strcat(binname,".bin");
  2545.     while(!stricmp(sourcename,binname))
  2546.         strcat(binname,"1");
  2547.  
  2548.     strcpy(labname,"L_");
  2549.     strcat(labname,itohex((long)FindTask(0),8)); /* Namen fuer Zwischenfile */
  2550.  
  2551.  
  2552.     if (!sourcetype) sourcetype = AutoScan();  /* Filetyp herausfinden */
  2553.     if (sourcetype == 1) relocmax=1;
  2554.  
  2555.     LabelAdr    = GetPMem(LabelMax*4);
  2556.     RelocAdr    = GetPMem(relocmax*4);
  2557.     RelocAdr[0] = 1; /* Marke, falls keine Relokationen vorliegen */
  2558.     RelocOff    = GetPMem(relocmax*4);
  2559.     RelocVal    = GetPMem(relocmax*4);
  2560.     RelocMod    = GetPMem(relocmax*4);
  2561.     SymbolName  = GetPMem(SymbolMax*sizeof(UBYTE *));
  2562.     SymbolValue = GetPMem(SymbolMax*sizeof(ULONG));
  2563.     CodeArea1   = GetPMem(CodeAreaMax*sizeof(ULONG));
  2564.     CodeArea2   = GetPMem(CodeAreaMax*sizeof(ULONG));
  2565.     CNFArea1    = GetPMem(CNFAreaMax*sizeof(ULONG));
  2566.     CNFArea2    = GetPMem(CNFAreaMax*sizeof(ULONG));
  2567.     CodeAdr     = GetPMem(CodeAdrMax*sizeof(ULONG));
  2568.  
  2569.     if (sourcetype == 2 || sourcetype == 3) {
  2570.         if (!(sourcefile = fopen(sourcename,"r")))
  2571.             ExitPrg("Can't open %s\n",sourcename);
  2572.         if (!(binfile = fopen(binname,"w")))
  2573.             ExitPrg("Can't open %s\n",binname);
  2574.     }
  2575.     if (sourcetype == 1) ReadBinary();
  2576.     if (sourcetype == 2) ReadExecutable();
  2577.     if (sourcetype == 3) ReadObject();
  2578.     if (basesec >= modulcount && basesec != -1)
  2579.         ExitPrg("There aren't so many sections (%ld).\n",basesec);
  2580.  
  2581.     if (sourcefile) fclose(sourcefile);
  2582.     if (binfile)    fclose(binfile);
  2583.     binfile = sourcefile = 0;
  2584.  
  2585.     prglen = FileLength(binname);
  2586.  
  2587.     if (!(binfile = fopen(binname,"r")))
  2588.         ExitPrg("Can't open %s\n",binname);
  2589.     if (!(labfile = fopen(labname,"w")))
  2590.         ExitPrg("Can't open %s\n",labname);
  2591.  
  2592.     LabelNum    = GetPMem(modulcount*sizeof(ULONG));
  2593.     XRefListe   = GetPMem(LabX_len*sizeof(ULONG));
  2594.     buffer      = GetPMem(prglen+4);
  2595.  
  2596.  
  2597.     if ((fread(buffer,1,prglen,binfile)) != prglen)
  2598.         ExitPrg("Can't read all data!\n");
  2599.  
  2600.     prgende = prgstart + prglen;
  2601.  
  2602.     if (pflags&CONFIG) ReadConfig();
  2603.  
  2604.     prgende = prgstart + prglen;
  2605.  
  2606.     adrlen=sprintf(zwbuf,"%x",prgende);
  2607.  
  2608.     if (codeentry >= prgende) ExitPrg("ERROR: Entry(=$%08X) is out of range!\n",codeentry);
  2609.     if (codeentry < prgstart) codeentry=prgstart;
  2610.  
  2611.     printf("SOURCE : %s\n",sourcename);
  2612.     printf("TARGET : %s\n",targetname);
  2613.     if (pflags&KEEP_BINARY)
  2614.         printf("BINARY : %s\n",binname);
  2615.     if (pflags&CONFIG)
  2616.         printf("CONFIG : %s\n",configname);
  2617.     for(i=0;i<5;i++)
  2618.         if (CPUTYPE&(1<<i))
  2619.             printf("MACHINE: %s\n",cpuname[i]);
  2620.     printf("OFFSET : $%08X\n",prgstart);
  2621.  
  2622.  
  2623. }
  2624. void ExitPrg(char *errtext, ...)
  2625. {
  2626. va_list arguments;
  2627. ULONG i;
  2628.  
  2629.     onbreak(0); /* Break-Trap ausklinken */
  2630.  
  2631.     if (errtext) {
  2632.         va_start(arguments,errtext);
  2633.         vprintf(errtext,arguments);
  2634.         fprintf(stderr, "\n");
  2635.         va_end(arguments);
  2636.     }
  2637.  
  2638.     if (sourcefile) fclose(sourcefile);
  2639.     if (binfile)    fclose(binfile);
  2640.     if (targetfile) fclose(targetfile);
  2641.     if (labfile)    fclose(labfile);
  2642.     if (configfile) fclose(configfile);
  2643.  
  2644.     if (labname[0]) {
  2645.         if (labfile = fopen(labname,"r")) {
  2646.             fclose(labfile);
  2647.             remove(labname);
  2648.         }
  2649.     }
  2650.     if (!(pflags&KEEP_BINARY) && binname[0]) {
  2651.         if (binfile = fopen(binname,"r")) {
  2652.             fclose(binfile);
  2653.             remove(binname);
  2654.         }
  2655.     }
  2656.  
  2657.     if (modulstrt) {
  2658.         for(i=0;i<modulcount;i++) {
  2659.             if (modulstrt[i]) Freemem(modulstrt[i],modultab[i]);
  2660.         }
  2661.         Freemem(modulstrt,modulcount*sizeof(ULONG *));
  2662.     }
  2663.  
  2664.     if (memtype)    Freemem(memtype  ,modulcount*sizeof(UWORD));
  2665.     if (modultab)   Freemem(modultab ,modulcount*sizeof(ULONG));
  2666.     if (modultype)  Freemem(modultype,modulcount*sizeof(ULONG));
  2667.     if (moduloffs)  Freemem(moduloffs,modulcount*sizeof(ULONG));
  2668.     if (labelbuf)   Freemem(labelbuf,labc1*sizeof(ULONG));
  2669.     if (buffer)     Freemem(buffer,prglen+4);
  2670.     if (RelocAdr)   Freemem(RelocAdr,relocmax*sizeof(ULONG));
  2671.     if (RelocMod)   Freemem(RelocMod,relocmax*sizeof(ULONG));
  2672.     if (RelocOff)   Freemem(RelocOff,relocmax*sizeof(LONG));
  2673.     if (RelocVal)   Freemem(RelocVal,relocmax*sizeof(ULONG));
  2674.     if (LabelNum)   Freemem(LabelNum,modulcount*sizeof(ULONG));
  2675.     if (LabelAdr)   Freemem(LabelAdr,LabelMax*sizeof(ULONG));
  2676.     if (LabelAdr2)  Freemem(LabelAdr2,(LabelMax+1)*sizeof(ULONG));
  2677.     if (XRefListe)  Freemem(XRefListe,LabX_len*sizeof(ULONG));
  2678.     if (RelocBuffer)Freemem(RelocBuffer,RelocNumber*sizeof(ULONG));
  2679.     if (DRelocBuffer)Freemem(DRelocBuffer,RelocNumber*sizeof(UWORD));
  2680.     if (CodeArea1)  Freemem(CodeArea1,CodeAreaMax*sizeof(ULONG));
  2681.     if (CodeArea2)  Freemem(CodeArea2,CodeAreaMax*sizeof(ULONG));
  2682.     if (CNFArea1)   Freemem(CNFArea1,CNFAreaMax*sizeof(ULONG));
  2683.     if (CNFArea2)   Freemem(CNFArea2,CNFAreaMax*sizeof(ULONG));
  2684.     if (CodeAdr)    Freemem(CodeAdr,CodeAdrMax*sizeof(ULONG));
  2685.     if (SymbolCount) {
  2686.         for(i=0;i<SymbolCount;i++)
  2687.             if (SymbolName[i]) Freemem(SymbolName[i], strlen(SymbolName[i])+1);
  2688.         if (SymbolValue) Freemem(SymbolValue, SymbolCount*sizeof(ULONG));
  2689.         if (SymbolName)  Freemem(SymbolName,  SymbolCount*sizeof(UBYTE *));
  2690.     }
  2691.  
  2692.     while (list.lh_TailPred != (struct Node *)&list)
  2693.         Freemem(RemHead(&list),sizeof(struct Node));
  2694.  
  2695.     if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  2696.  
  2697.     exit(0);
  2698. }
  2699. /* 1. Pass : find out possible addresses for labels */
  2700. void DPass1(void)
  2701. {
  2702. UWORD dummy;
  2703. ULONG i,area,end;
  2704.  
  2705.     PASS = 1;
  2706.     prgcount = 0;
  2707.     nextreloc= 0;
  2708.     modulcnt =-1;
  2709.  
  2710.  
  2711.     for(area=0;area<CodeAreas;area++) {
  2712.  
  2713.         while ((moduloffs[modulcnt+1] == CodeArea1[area]) && ((modulcnt+1) < modulcount))
  2714.             modulcnt++;
  2715.  
  2716.         /* HERE BEGINS THE CODE PART OF PASS 1 */
  2717.         /***************************************/
  2718.  
  2719.         CodeAreaEnd = (CodeArea2[area]-prgstart)/2;
  2720.  
  2721.         while(prgcount < CodeAreaEnd) {
  2722.  
  2723.             WriteLabel1(prgstart+prgcount*2);
  2724.  
  2725.             if (RelocAdr[nextreloc] == (prgcount*2 + prgstart)) {
  2726.                 nextreloc++;
  2727.                 prgcount += 2;
  2728.                 continue;
  2729.             }
  2730.             pc = prgcount;
  2731.             sigw=(UWORD)buffer[prgcount++];
  2732.  
  2733.  
  2734.             GetOpcode();
  2735.             if (flags[opcnumber]&0x20) {
  2736.                 extra=buffer[prgcount];
  2737.                 if (P1WriteReloc()) continue;
  2738.             }
  2739.  
  2740.             if (opcnumber == OPC_CMPI) {
  2741.                 if (CPUTYPE&M020UP) destadr[opcnumber]=0x0bfe;
  2742.                 else destadr[opcnumber]=0x0bf8;
  2743.             } else if (opcnumber==OPC_TST) {
  2744.                 if (CPUTYPE&M020UP) sourceadr[opcnumber]=0x0fff;
  2745.                 else sourceadr[opcnumber]=0x0bf8;
  2746.             } else if (opcnumber==OPC_BITFIELD) {
  2747.                 dummy=(sigw&0x0700)>>8;
  2748.                 if (dummy==2 || dummy==4 || dummy==6 || dummy==7) sourceadr[opcnumber]=0x0a78;
  2749.                 else sourceadr[opcnumber]=0x0a7e;
  2750.             } else if (opcnumber==OPC_C2) {
  2751.                 if (extra&0x07ff) adrmode=NOADRMODE;
  2752.                 else {
  2753.                     reg1=(extra&0x7000)>>12;
  2754.                     if (extra&0x8000) destadr[opcnumber]=0xa001;
  2755.                     else destadr[opcnumber]=0xa000;
  2756.                 }
  2757.             } else if (opcnumber==OPC_MOVE162) {
  2758.                 switch ((buffer[prgcount]&0x0018)>>3) {
  2759.                     case 0: /* (An)+,(xxx).L */
  2760.                         sourceadr[opcnumber]=0x8003;
  2761.                         destadr[opcnumber]  =0x8008;
  2762.                         break;
  2763.                     case 1: /* (xxx).L,(An)+ */
  2764.                         sourceadr[opcnumber]=0x8008;
  2765.                         destadr[opcnumber]  =0x8003;
  2766.                         break;
  2767.                     case 2: /* (An) ,(xxx).L */
  2768.                         sourceadr[opcnumber]=0x8002;
  2769.                         destadr[opcnumber]  =0x8008;
  2770.                         break;
  2771.                     case 3: /* (xxx).L, (An) */
  2772.                         sourceadr[opcnumber]=0x8008;
  2773.                         destadr[opcnumber]  =0x8002;
  2774.                         break;
  2775.                 }
  2776.             } else if (opcnumber==OPC_MOVES) {
  2777.                 if (extra&0x0800) {
  2778.                     sourceadr[opcnumber]=0x8022;
  2779.                     destadr[opcnumber]  =0x03f8;
  2780.                 }
  2781.                 else {
  2782.                     sourceadr[opcnumber]=0x03f8;
  2783.                     destadr[opcnumber]  =0x8022;
  2784.                 }
  2785.             }
  2786.  
  2787.             if ((flags[opcnumber]&0x40) && extens==3) adrmode=NOADRMODE;
  2788.  
  2789.             if (sourceadr[opcnumber])
  2790.                 if (DoAdress1(sourceadr[opcnumber])) continue;
  2791.             if (destadr[opcnumber]) {
  2792.                 if (opcnumber==OPC_MOVEB || opcnumber==OPC_MOVEW || opcnumber==OPC_MOVEL) {
  2793.                     adrmode=((sigw&0x01c0)>>3)|reg1;
  2794.                     if (adrmode<0x38) adrmode=(adrmode>>3);
  2795.                     else adrmode=7+reg1;
  2796.                     reg2=reg1;
  2797.                 }
  2798.                 if (DoAdress1(destadr[opcnumber])) continue;
  2799.                 else {
  2800.                     if (opcnumber==OPC_LEA || opcnumber==OPC_MOVEAL) {
  2801.                         if (pflags&BASEREG1) {
  2802.                             if (adrmode2==1 && reg1==basereg)
  2803.                                 printf("BASEREG\t%08X: A%hd\n",pc*2+prgstart,basereg);
  2804.                         }
  2805.                     }
  2806.                 }
  2807.             }
  2808.  
  2809.             if (prgcount > CodeAreaEnd)
  2810.                 printf("P1 Watch out: prgcount*2(=%08x) > (prgende-prgstart)(=%08x)\n",prgcount*2,prgende-prgstart);
  2811.  
  2812.         }
  2813.  
  2814.  
  2815.         while ((moduloffs[modulcnt+1] == CodeArea2[area]) && ((modulcnt+1) < modulcount))
  2816.             modulcnt++;
  2817.  
  2818.  
  2819.         /* HERE BEGINS THE DATA PART OF PASS 1 */
  2820.         /***************************************/
  2821.  
  2822.         if ((area+1)<CodeAreas) 
  2823.             end = CodeArea1[area+1];
  2824.         else
  2825.             end = prgende;
  2826.  
  2827.         for(i=CodeArea2[area];i<end;i++) {
  2828.             /* WriteLabel1(i); */
  2829.             if (RelocAdr[nextreloc] == i) {
  2830.                 nextreloc ++;
  2831.                 i += 3;
  2832.             }
  2833.         }
  2834.         prgcount = (end-prgstart)/2;
  2835.     }
  2836.  
  2837.     fprintf(stderr,"Pass 1: 100%%\n");
  2838.     if (relocount != nextreloc) printf("relocount=%lu nextreloc=%lu\n",relocount,nextreloc);
  2839.     fclose(labfile);labfile=0;
  2840. }
  2841. void WriteLabel1(ULONG adr)
  2842. {
  2843. static UWORD linecount=200;
  2844.     /* Prozentausgabe */
  2845.     if (linecount++ >= 200) {
  2846.         fprintf(stderr,"Pass 1: %3d%%\r",((adr-prgstart)*100)/prglen);
  2847.         fflush(stderr);
  2848.         linecount=0;
  2849.     }
  2850.  
  2851.     if ((fwrite(&adr,4,1,labfile) != 1))
  2852.         ExitPrg("Write error !\n");
  2853.     labc1++;
  2854. }
  2855. int P1WriteReloc()
  2856. {
  2857.     if (RelocAdr[nextreloc] == (prgcount*2 + prgstart)) {
  2858.         prgcount=pc+1;
  2859.         return(-1);
  2860.     }
  2861.     else {
  2862.         prgcount++;
  2863.         return(0);
  2864.     }
  2865. }
  2866. UWORD NewAdrModes1(UWORD mode, UWORD reg)
  2867. /* AdrType :  6 --> Baseregister An */
  2868. /*           10 --> PC-relative     */
  2869. {
  2870. UWORD buf=buffer[prgcount];
  2871. UWORD bdsize;
  2872. UWORD odsize;
  2873. UWORD iis;
  2874. UWORD is;
  2875. UWORD operand,square1,square2;
  2876. LONG  adr;
  2877.  
  2878.     if (P1WriteReloc()) return((UWORD)-1);
  2879.  
  2880.     /* Achtung: Ungerade Offsets werden vom A68K nicht angenommen */
  2881.     if (CPUTYPE&(M68000|M68010)) {
  2882.         if (buf&0x0700) return(NOADRMODE);
  2883.         else {
  2884.             if (mode==10) {
  2885.                 adr = ((prgcount-1)*2+prgstart+(BYTE)buf);
  2886.                 if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  2887.                     return(NOADRMODE);
  2888.                 InsertLabel(adr);
  2889. /*
  2890.                 LabAdr=adr;
  2891.                 LabAdrFlag=1;
  2892. */
  2893.             }
  2894.         }
  2895.     }
  2896.     else {
  2897.         if (buf&0x0100) { /* MC68020 (& up) FULL FORMAT */
  2898.             bdsize=(buf&0x0030)>>4;
  2899.             odsize=(buf&0x0003);
  2900.             iis   =(buf&0x0007);
  2901.             is    =(buf&0x0040)>>6;
  2902.             operand=square1=square2=0;
  2903.  
  2904.             if (mode==10) reg=0;
  2905.             if (buf&8)                 return(NOADRMODE);
  2906.             if (bdsize==0)             return(NOADRMODE);
  2907.             if (is==0 && iis==4)       return(NOADRMODE);
  2908.             if (is==1 && iis>=4)       return(NOADRMODE);
  2909. /*
  2910.             if (is==1 && (buf&0xfe00)) return(NOADRMODE);
  2911.             if (buf&0x0080 && reg!=0)  return(NOADRMODE);
  2912. */
  2913.             if (bdsize>1)               {operand|=1;square1|=1;}
  2914.             if (!(buf&0x0080))          {operand|=2;square1|=2;}
  2915.             if (buf&0x0080 && mode==10) {operand|=2;square1|=2;}
  2916.             if (is==0 || buf&0xF000) {
  2917.                 operand|=4;
  2918.                 if (iis<4) square1|=4;
  2919.             }
  2920.             if (odsize>1) operand|=8;
  2921.             if (iis!=0)   square2=square1;
  2922.             else          square1=0;
  2923.             operand&=~square1;
  2924.  
  2925.             if ((square1|operand)&1) {
  2926.                 if (bdsize==2) {
  2927.                     if (mode==10 && !(buf&0x0080)) {
  2928.                         adr = ((prgcount-1)*2+prgstart+(WORD)buffer[prgcount]);
  2929.                         if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  2930.                             return(NOADRMODE);
  2931.                         else {
  2932.                             if (P1WriteReloc()) return((UWORD)-1);
  2933.                             InsertLabel(adr);
  2934.                         }
  2935.                     }
  2936.                     else {
  2937.                         if (P1WriteReloc()) return((UWORD)-1);
  2938.                     }
  2939.                 }
  2940.                 if (bdsize==3) {
  2941.                     if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  2942.                         nextreloc++;
  2943.                         prgcount+=2;
  2944.                     }
  2945.                     else {
  2946.                         prgcount++;
  2947.                         if (mode==10 && !(buf&0x0080)) {
  2948.                             adr = ((prgcount-2)*2+prgstart+(buffer[prgcount-1]<<16)+buffer[prgcount]);
  2949.                             if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  2950.                                 return(NOADRMODE);
  2951.                             if (P1WriteReloc()) return((UWORD)-1);
  2952.                             InsertLabel(adr);
  2953.                         }
  2954.                         else {
  2955.                             if (P1WriteReloc()) return((UWORD)-1);
  2956.                         }
  2957.                     }
  2958.                 }
  2959. /*
  2960.                 square1&=~1;
  2961.                 operand&=~1;
  2962.                 if (square2 && !square1) {square2=0;}
  2963. */
  2964.             }
  2965. /*
  2966.             if ((square1|operand)&2) {
  2967.                 square1&=~2;
  2968.                 operand&=~2;
  2969.                 if (square2 && !square1) {square2=0;}
  2970.             }
  2971.             if ((square1|operand)&4) {
  2972.                 square1&=~4;
  2973.                 operand&=~4;
  2974.                 if (square2 && !square1) {square2=0;}
  2975.             }
  2976. */
  2977.             if (operand&8) {
  2978.                 if (odsize==2) {
  2979.                     if (P1WriteReloc()) return((UWORD)-1);
  2980.                 }
  2981.                 if (odsize==3) {
  2982.                     if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  2983.                         nextreloc++;
  2984.                         prgcount+=2;
  2985.                     }
  2986.                     else {
  2987.                         prgcount++;
  2988.                         if (P1WriteReloc()) return((UWORD)-1);
  2989.                     }
  2990.                 }
  2991.             }
  2992.         }
  2993.         else { /* MC68020 (& up) BRIEF FORMAT */
  2994.             if (mode==10) {
  2995.                 adr = ((prgcount-1)*2+prgstart+(BYTE)buf);
  2996.                 if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR)))
  2997.                     return(NOADRMODE);
  2998.                 InsertLabel(adr);
  2999. /*
  3000.                 LabAdr=adr;
  3001.                 LabAdrFlag=1;
  3002. */
  3003.             }
  3004.         }
  3005.     }
  3006.     return(mode);
  3007. }
  3008.  
  3009. /* This is for PASS 1 */
  3010. int DoAdress1(UWORD adrs)
  3011. {
  3012. UWORD mode=adrmode;
  3013. UWORD buf=buffer[prgcount];
  3014. UWORD reg,creg;
  3015. LONG  adr;
  3016.  
  3017.     if (mode!=NOADRMODE) {
  3018.         /* if (mode>0x30) mode=7+(mode&7); */
  3019.  
  3020.         if (adrs&0x2000) reg=reg1;
  3021.         else reg=reg2;
  3022.  
  3023.         if (adrs&0x8000) adrmode2=mode=adrs&0x00FF;
  3024.         else
  3025.             if ((adrs&0x0fff)==adrs)
  3026.                 if (!(adrs&(0x0800>>mode))) adrmode2=mode=NOADRMODE;
  3027.     }
  3028.  
  3029.     /* Adressierungsart bearbeiten */
  3030.     switch (mode) {
  3031.         case  1: /* Adressregister direkt */
  3032.                     /* Auf Adressregister kann nicht byteweise zugegriffen werden    */
  3033.                     /* Bei LEA ist extens == 0 (weil ungerade Adressen erlaubt sind) */
  3034.                     if (extens || opcnumber==OPC_LEA) {}
  3035.                     else mode=NOADRMODE;
  3036.                     break;
  3037.         case  5: /* (d16,An) Adressregister indirekt mit 16Bit-Offset */
  3038.                     /* Achtung: Ungerade Offsets werden vom A68K nicht angenommen */
  3039. /*
  3040.                     if (extens && buf&1) mode=NOADRMODE;
  3041.                     else {
  3042. */
  3043.                         if (P1WriteReloc()) return(-1);
  3044.                         if (pflags&BASEREG2 && reg==basereg) {
  3045.                             adr = prgstart+baseadr+(WORD)buf;
  3046.                             if (adr>(LONG)(moduloffs[basesec]+modultab[basesec]-2) || adr<(LONG)moduloffs[basesec]) {}
  3047.                             else {
  3048.                                 InsertLabel(adr);
  3049.                                 LabAdr=adr;
  3050.                                 LabAdrFlag=1;
  3051.                             }
  3052. /*                        } */
  3053.                     }
  3054.                     break;
  3055.         case  6: /* Adressreg. ind. mit Adressdistanz und Index */
  3056.         case 10: /* D8(PC,Xn) */
  3057.                     if ((mode=NewAdrModes1(mode,reg))==-1) return(-1);
  3058.                     break;
  3059.         case  7: /* Absolute Adresse 16Bit */
  3060.                     adr = (ULONG)((WORD)buf);
  3061.                     if (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR || opcnumber==OPC_BITSHIFT1))
  3062.                         mode=NOADRMODE;
  3063.                     else {
  3064.                         if (P1WriteReloc()) return(-1);
  3065.                         /* PEA  wegen den C-Proggies (Stackuebergabe) */
  3066.                         if (opcnumber != OPC_PEA) {
  3067.                             /* Bei Binaerfiles absolute Adr. evtl im Programmbereich */
  3068.                             if (sourcetype == 1 && prgstart && (adr >= prgstart && adr <= prgende)) {
  3069.                                 InsertLabel(adr);
  3070.                                 LabAdr=adr;
  3071.                                 LabAdrFlag=1;
  3072.                             }
  3073.                             else
  3074.                                 InsertXref(adr);
  3075.                         }
  3076.                     }
  3077.                     break;
  3078.         case  8: /* Absolute Adresse 32Bit */
  3079.                     adr = (ULONG)((buf<<16) + buffer[prgcount+1]);
  3080.                     if (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR || opcnumber==OPC_BITSHIFT1))
  3081.                         mode=NOADRMODE;
  3082.                     else {
  3083. /* printf(stderr,"RADR=%08x, adr=%08x\n",RelocAdr[nextreloc],(prgcount*2+prgstart)); */
  3084.                         if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  3085. /* printf("3 RelocAdr[%ld]=$%08X  RelocAdr[%ld]=$%08X\n",nextreloc,nextreloc+1,RelocAdr[nextreloc],RelocAdr[nextreloc+1]); */
  3086.                             LabAdr=adr;
  3087.                             LabAdrFlag=1;
  3088.                             nextreloc++;
  3089.                         }
  3090.                         else {
  3091.                             /* PEA  wegen den C-Proggies (Stackuebergabe) */
  3092.                             if (opcnumber != OPC_PEA) {
  3093.                                 if (sourcetype == 1 && prgstart && (adr >= prgstart && adr <= prgende)) {
  3094.                                     InsertLabel(adr);
  3095.                                     LabAdr=adr;
  3096.                                     LabAdrFlag=1;
  3097.                                 }
  3098.                                 else
  3099.                                     InsertXref(adr);
  3100.                             }
  3101.                         }
  3102.                         prgcount+=2;
  3103.                     }
  3104.                     break;
  3105.         case  9: /* PC - Relativ */
  3106.                     adr = (prgcount*2+prgstart+(WORD)buf);
  3107.                     if (adr>=(LONG)(moduloffs[modulcnt]+modultab[modulcnt]) || adr<(LONG)(moduloffs[modulcnt]-8) || (adr&1 && (extens || opcnumber==OPC_JMP || opcnumber==OPC_JSR))) mode=NOADRMODE;
  3108.                     else {
  3109.                         if (P1WriteReloc()) return(-1);
  3110.                         InsertLabel(adr);
  3111.                         LabAdr=adr;
  3112.                         LabAdrFlag=1;
  3113.                     }
  3114.                     break;
  3115.         case 11: 
  3116.                     if (adrs==sourceadr[opcnumber]) {
  3117.                         if (extens!=3) {
  3118.                             if (extens==0) {
  3119.                                 if (buf&0xFF00) mode=NOADRMODE;
  3120.                                 else {
  3121.                                     if (P1WriteReloc()) return(-1);
  3122.                                 }
  3123.                             }
  3124.                             if (extens==1) {
  3125.                                 if (P1WriteReloc()) return(-1);
  3126.                             }
  3127.                             if (extens==2) {
  3128.                                 if (RelocAdr[nextreloc]==(prgcount*2+prgstart+2))
  3129.                                     mode=NOADRMODE;
  3130.                                 else {
  3131.                                     if (RelocAdr[nextreloc]==(prgcount*2+prgstart)) {
  3132. /* printf("4 RelocAdr[%ld]=$%08X  RelocAdr[%ld]=$%08X\n",nextreloc,nextreloc+1,RelocAdr[nextreloc],RelocAdr[nextreloc+1]); */
  3133.                                         nextreloc++;
  3134.                                     }
  3135.                                     prgcount += 2;
  3136.                                 }
  3137.                             }
  3138.                         }
  3139.                         else mode=NOADRMODE;
  3140.                     }
  3141.                     else if (extens==2) mode=NOADRMODE; /* d=immediate long */
  3142.                     break;
  3143.         case 18: /* DBRA, DB.. */
  3144.                     adr = (prgcount*2+prgstart+(WORD)buf);
  3145.                     if (adr>(LONG)(moduloffs[modulcnt]+modultab[modulcnt]-2) || adr<(LONG)moduloffs[modulcnt] || adr&1 || !buf)
  3146.                         mode=NOADRMODE;
  3147.                     else {
  3148.                         if (P1WriteReloc()) return(-1);
  3149.                         InsertLabel(adr);
  3150.                         LabAdr=adr;
  3151.                         LabAdrFlag=1;
  3152.                     }
  3153.                     break;
  3154.         case 21: /* Bcc */
  3155.                     if ((sigw&0x00ff)==0x00ff) {
  3156.                         if (CPUTYPE&M020UP) {
  3157.                             displace=(buf<<16)|buffer[prgcount+1];
  3158.                             if (displace!=0 && displace!=2) {
  3159.                                 displace+=prgcount*2;
  3160.                                 if (P1WriteReloc()) return(-1);
  3161.                                 if (P1WriteReloc()) return(-1);
  3162.                             }
  3163.                             else mode=NOADRMODE;
  3164.                         }
  3165.                         else mode=NOADRMODE;
  3166.                     } else if ((sigw&0x00ff)==0x0000) {
  3167.                         if (buf) {
  3168.                             displace=(prgcount*2+(WORD)(buf));
  3169.                             if (P1WriteReloc()) return(-1);
  3170.                         }
  3171.                         else mode=NOADRMODE;
  3172.                     } else {
  3173.                         displace=(prgcount*2+(char)(sigw&0x00ff));
  3174.                     }
  3175.                     adr = prgstart+displace;
  3176.                     if (adr>(LONG)(moduloffs[modulcnt]+modultab[modulcnt]-2) || adr<(LONG)moduloffs[modulcnt] || adr&1)
  3177.                         mode=NOADRMODE;
  3178.                     else {
  3179.                         InsertLabel(adr);
  3180.                         LabAdr=adr;
  3181.                         LabAdrFlag=1;
  3182.                     }
  3183.                     break;
  3184.         case 22: /* LINK */
  3185.                     if (buf&1) mode=NOADRMODE;
  3186.                     else {
  3187.                         if (P1WriteReloc()) return(-1);
  3188.                     }
  3189.                     break;
  3190.         case 24: /* STOP */
  3191.                     if (P1WriteReloc()) return(-1);
  3192.                     break;
  3193.         case 23: /* BTST,BCLR,... IMMEDIATE®ISTER,SOURCEOP ONLY */
  3194.                     if (!extens) destadr[opcnumber]=0x0bfe; /* BTST */
  3195.                     else destadr[opcnumber]=0x0bf8;         /* sonstige B... */
  3196.                     if (sigw&0x0100) {}
  3197.                     else {
  3198.                         if (P1WriteReloc()) return(-1);
  3199.                         if (sigw&0x0038) {
  3200.                             if (buf&0xFFF8) mode=NOADRMODE;
  3201.                         }
  3202.                         else {
  3203.                             if (buf&0xFFE0) mode=NOADRMODE;
  3204.                         }
  3205.                     }
  3206.                     extens=0; /* Set extension to BYTE (undefined before) */
  3207.                     break;
  3208.         case 25: /* BITFIELD */
  3209.                     reg=(extra&0x07c0)>>6;
  3210.                     if (extra&0x0800) {
  3211.                         if (reg>7) mode=NOADRMODE;
  3212.                     }
  3213.                     reg=(extra&0x001F);
  3214.                     if (extra&0x0020) {
  3215.                         if (reg>7) mode=NOADRMODE;
  3216.                     }
  3217.                     if (((sigw&0x0700)>>8)&1) {
  3218.                         /* BFEXTU, BFEXTS, BFFFO, BFINS */
  3219.                         if (extra&0x8000) mode=NOADRMODE;
  3220.                     }
  3221.                     else {
  3222.                         if (extra&0xF000) mode=NOADRMODE;
  3223.                     }
  3224.                     break;
  3225.         case 27: /* CAS2  SOURCE/DESTINATION */
  3226.                     buf=buffer[prgcount];
  3227.                     if (P1WriteReloc()) return(-1);
  3228.                     extens=(sigw&0x0600)>>9;
  3229.                     if (extens==0 || extens==1) mode=NOADRMODE;
  3230.                     else extens-=1;
  3231.                     if (buf&0x0e38 || extra&0x0e38) mode=NOADRMODE;
  3232.                     break;
  3233.         case 28: /* CAS   SOURCE */
  3234.                     extens=(sigw&0x0600)>>9;
  3235.                     if (extens==0) mode=NOADRMODE;
  3236.                     else extens-=1;
  3237.                     if (extra&0xfe38) mode=NOADRMODE;
  3238.                     break;
  3239.         case 29: /* DIVIDE/MULTIPLY LONG  SIGNED/UNSIGNED */
  3240.                     if (extra&0x83f8) mode=NOADRMODE;
  3241.                     else {
  3242.                         reg=(extra&0x7000)>>12;
  3243.                         if (reg==(extra&0x0007)) {
  3244.                             if (opcnumber==OPC_MULL) mode=NOADRMODE;
  3245.                         }
  3246.                     }
  3247.                     break;
  3248.         case 30: /* LINK LONG */
  3249.                     displace=(buf<<16)|buffer[prgcount+1];
  3250.                     if (displace&1) mode=NOADRMODE;
  3251.                     else {
  3252.                         if (P1WriteReloc()) return(-1);
  3253.                         if (P1WriteReloc()) return(-1);
  3254.                     }
  3255.                     break;
  3256.         case 31: /* MOVE16 POSTINCREMENT ONLY (DESTINATION) */
  3257.                     if ((buf&0x8fff)!=0x8000) mode=NOADRMODE;
  3258.                     else {
  3259.                         if (P1WriteReloc()) return(-1);
  3260.                     }
  3261.                     break;
  3262.         case 32: /* CINV & CPUSH */
  3263.                     destadr[opcnumber]=0x8002;
  3264.                     switch ((sigw&0x0018)>>3) {
  3265.                         case 0:
  3266.                             mode=NOADRMODE;
  3267.                             break;
  3268.                         case 3:
  3269.                             if (sigw&7) mode=NOADRMODE;
  3270.                             else {
  3271.                                 destadr[opcnumber]=0x0000;
  3272.                             }
  3273.                             break;
  3274.                     }
  3275.                     break;
  3276.         case 33: /* MOVEC */
  3277.                     if (P1WriteReloc()) return(-1);
  3278.                     reg =(buf&0x7000)>>12;
  3279.                     creg=buf&0x0fff;
  3280.                     if (creg&0x07f8) mode=NOADRMODE;
  3281.                     else {
  3282.                         if (creg&0x0800) creg=(creg%8)+9;
  3283.                         if (CPUTYPE&cregflag[creg]) {}
  3284.                         else mode=NOADRMODE;
  3285.                     }
  3286.                     break;
  3287.         case 34: /* MOVES */
  3288.                     if (extra&0x07ff) mode=NOADRMODE;
  3289.                     break;
  3290.     }
  3291.     if (prgcount > CodeAreaEnd) mode=NOADRMODE;
  3292.     if (mode==NOADRMODE) {
  3293.         prgcount = pc+1;
  3294.         return(-1);
  3295.     }
  3296.     return (0);
  3297. }
  3298. int AutoScan(void)
  3299. {
  3300. FILE  *file;
  3301. ULONG  seg;
  3302. ULONG  dummy;
  3303.  
  3304.     if (!(file = fopen(sourcename,"r")))
  3305.         ExitPrg("Can't open %s\n",sourcename);
  3306.  
  3307.     /* Header des Sourcefiles pruefen */
  3308.     fread(&dummy,4,1,file);
  3309.     fclose(file);
  3310.  
  3311.     if (dummy == 0x03F3) { /* HUNK_HEADER --> Executablefile */
  3312.         if (seg = LoadSeg(sourcename)) {
  3313.             UnLoadSeg(seg);
  3314.             if (pflags&SHOW_RELOCINFO) printf("\nExecutable (%s)....:\n",sourcename);
  3315.             return (2);
  3316.         }
  3317.     }
  3318.     if (dummy == 0x3E7) { /* HUNK_UNIT --> Objectfile */
  3319.         if (pflags&SHOW_RELOCINFO) printf("\nObject (%s)........:\n",sourcename);
  3320.         return(3);
  3321.     }
  3322.     if (pflags&SHOW_RELOCINFO) printf("\nBinary (%s)........:\n",sourcename);
  3323.     return (1);
  3324. }
  3325. void ReadBinary(void)
  3326. {
  3327.     pflags |= KEEP_BINARY;
  3328.  
  3329.     modulcount = 1; /* Nur 1 Modul */
  3330.     memtype    = GetPMem(sizeof(UWORD));
  3331.     modultab   = GetPMem(sizeof(ULONG));
  3332.     modultype  = GetPMem(sizeof(ULONG));
  3333.     moduloffs  = GetPMem(sizeof(ULONG));
  3334.  
  3335.     modultab[0]  = FileLength(sourcename);
  3336.     moduloffs[0] = prgstart;
  3337.     modultype[0] = 0x03E9; /* HUNK_CODE */
  3338.  
  3339.  
  3340.    LastModul   = 1;
  3341.    FirstModul  = 0;
  3342.  
  3343.     strcpy(binname,sourcename);
  3344. }
  3345. void ReadObject(void)
  3346. {
  3347. ULONG hunk,length,i;
  3348. ULONG dummy;
  3349.  
  3350.     fseek(sourcefile,4,SEEK_SET);
  3351.     ReadSymbol(sourcefile,0,0);
  3352.     if (pflags&SHOW_RELOCINFO) printf("  Unit    : %s\n",StdName);
  3353.  
  3354.  
  3355.     while (fread(&hunk,4,1,sourcefile) == 1) {  /* Modulart (Code,Data,...) */
  3356.  
  3357.         if ((hunk>>30) == 3) fread(&length,4,1,sourcefile); /* Aufwaertskompatibel */
  3358.         hunk &= 0x0000FFFF;
  3359.  
  3360.         switch (hunk) {
  3361.             case 0x03E9: /* CODE */
  3362.             case 0x03EA: /* DATA */
  3363.             case 0x03EB: /* BSS  */
  3364.                     modulcount++; /* Anzahl der Module +1 */
  3365.                     fread(&length,4,1,sourcefile); /* Laenge des Moduls */
  3366.                     node=GetPMem(sizeof(struct Node));
  3367.                     node->ln_Name = (char *)length;
  3368.                     AddTail(&list,node);
  3369.                     if (hunk != 0x03EB)      /* Nur bei Code und Data */
  3370.                         fseek(sourcefile,length*4,SEEK_CUR); /* Laenge ueberlesen */
  3371.                 break;
  3372.             case 0x03F7: /* HUNK_DREL32  */
  3373.             case 0x03F8: /* HUNK_DREL16  */
  3374.             case 0x03F9: /* HUNK_DREL8   */
  3375.             case 0x03EC: /* HUNK_RELOC32 */
  3376.             case 0x03ED: /* HUNK_RELOC16 */
  3377.             case 0x03EE: /* HUNK_RELOC8  */
  3378.                     do {
  3379.                         /* read number of relocations */
  3380.                         if ((fread(&length,4,1,sourcefile)) != 1) break;
  3381.                         if (length) fseek(sourcefile,(length+1)*4,SEEK_CUR);
  3382.                     } while (length);
  3383.                 break;
  3384.             case 0x03F2: /* HUNK_END   */
  3385.                 break;
  3386.             case 0x03E8: /* HUNK_NAME */
  3387.                     fread(&length,4,1,sourcefile);
  3388.                     fseek(sourcefile,length*4,SEEK_CUR);
  3389.                 break;
  3390.             case 0x03F1: /* HUNK_DEBUG */
  3391.                     fread(&length,4,1,sourcefile);
  3392.                     fseek(sourcefile,length*4,SEEK_CUR);
  3393.                 break;
  3394.             case 0x03F0: /* HUNK_SYMBOL */
  3395.                     do {
  3396.                         if ((fread(&length,4,1,sourcefile)) != 1) break;
  3397.                         if (length) fseek(sourcefile,(length+1)*4,SEEK_CUR);
  3398.                     } while (length);
  3399.                 break;
  3400.             case 0x03EF: /* HUNK_EXT */
  3401.                     do {
  3402.                         UBYTE type;
  3403.  
  3404.                         if ((fread(&length,4,1,sourcefile)) != 1) break;
  3405.                         type = length>>24;
  3406.                         dummy=length;
  3407.                         length &= 0x00FFFFFF;
  3408.                         if (dummy) {
  3409.                             switch (type) {
  3410.                                 case 0: /* EXT_SYMB */
  3411.                                 case 1: /* EXT_DEF  */
  3412.                                 case 2: /* EXT_ABS  */
  3413.                                 case 3: /* EXT_RES  */
  3414.                                 case 130: /* EXT_COMMON */
  3415.                                     fseek(sourcefile,(length+1)*4,SEEK_CUR);
  3416.                                     if (type==130) {
  3417.                                         fread(&length,4,1,sourcefile);
  3418.                                         fseek(sourcefile,length*4,SEEK_CUR);
  3419.                                     }
  3420.                                     break;
  3421.                                 case 129: /* EXT_REF32  */
  3422.                                 case 131: /* EXT_REF16  */
  3423.                                 case 132: /* EXT_REF8   */
  3424.                                 case 133: /* EXT_DEXT32 */
  3425.                                 case 134: /* EXT_DEXT16 */
  3426.                                 case 135: /* EXT_DEXT8  */
  3427.                                     fseek(sourcefile,length*4,SEEK_CUR);
  3428.                                     fread(&length,4,1,sourcefile);
  3429.                                     fseek(sourcefile,length*4,SEEK_CUR);
  3430.                                     break;
  3431.                                 default:
  3432.                                     ExitPrg("Unknown HUNK_EXT sub-type=%d !\n",type);
  3433.                                     break;
  3434.                             }
  3435.                         }
  3436.                     } while (dummy);
  3437.                 break;
  3438.             default:
  3439.                     ExitPrg("Hunk...:%08x NOT SUPPORTED.\n",hunk);
  3440.                 break;
  3441.  
  3442.         } /* Ende - Switch() */
  3443.  
  3444.     } /* Naechstes Modul einlesen und relocieren. */
  3445.  
  3446.     if (pflags&SHOW_RELOCINFO) printf("  Modules : %d\n",modulcount);
  3447.  
  3448.     memtype  = GetPMem(modulcount*sizeof(UWORD));
  3449.     modultab = GetPMem(modulcount*sizeof(ULONG));
  3450.     modultype= GetPMem(modulcount*sizeof(ULONG));
  3451.     moduloffs= GetPMem(modulcount*sizeof(ULONG));
  3452.     modulstrt= GetPMem(modulcount*sizeof(ULONG *));
  3453.  
  3454.     for(i=0;i<modulcount;i++) {
  3455.         if (!(node=RemHead(&list)))
  3456.             ExitPrg("Trouble with exec RemHead !\n");
  3457.         modultab[i] = (ULONG)node->ln_Name;
  3458.         Freemem(node,sizeof(struct Node));
  3459.     }
  3460.  
  3461.     fseek(sourcefile,4L,SEEK_SET);
  3462.     ReadSymbol(sourcefile,0,0);
  3463.  
  3464.    LastModul  = modulcount - 1;
  3465.    FirstModul = 0;
  3466.  
  3467.     ExamineHunks();
  3468. }
  3469. void ReadExecutable(void)
  3470. {
  3471. ULONG dummy;
  3472.  
  3473.     fseek(sourcefile,4L,SEEK_SET);
  3474.     /* Librarynamen (wird normal nicht genutzt) ueberlesen */
  3475.     while (dummy=ReadSymbol(sourcefile,0,0))
  3476.         printf("  Library : %s\n",StdName);
  3477.  
  3478.     /* Anzahl der Module einlesen */
  3479.     fread(&modulcount,4,1,sourcefile);
  3480.  
  3481.     if (pflags&SHOW_RELOCINFO) printf("  Modules : %d\n",modulcount);
  3482.  
  3483.     /* First und Last einlesen */
  3484.     fread(&FirstModul,4,1,sourcefile);
  3485.     fread(&LastModul,4,1,sourcefile);
  3486.  
  3487.     if (FirstModul) ExitPrg("Can't handle firstmodul != 0 !!\n");
  3488.  
  3489.     memtype  = GetPMem(modulcount*sizeof(UWORD));
  3490.     modultab = GetPMem(modulcount*sizeof(ULONG));
  3491.     modultype= GetPMem(modulcount*sizeof(ULONG));
  3492.     moduloffs= GetPMem(modulcount*sizeof(ULONG));
  3493.     modulstrt= GetPMem(modulcount*sizeof(ULONG *));
  3494.  
  3495.     /* Modultabelle (Modullaengen) einlesen */
  3496.     fread(modultab,4,(LastModul-FirstModul+1),sourcefile);
  3497.  
  3498.     ExamineHunks();
  3499.  
  3500. }
  3501.